Sunday, September 05, 2004

Python x Java

I am not really trying to analyse it deeper, just put random thoughts regarding what I have felt about these two languages.

Python does not require that you declare data types, although requires that you initialize it. As I am used to declare any variable, I had to change a little bit to enjoy the Python way. I did like the numarray package, as it provides lots of functions for manipulating arrays. For engineering purposes, having a strong array library is very important.

The numarray API already overloads operators like +, * and math operations, which is also great. If we see this way, Python is perfect for science. OOP programming in Python is easy, although I saw no references yet to the use of polymorphism in the same way we can do in Java or C++. For ex:

Foo * f = new Foo( );
Foo *f2;

Foo f = new Foo( );
Foo f2;

In either languages if you do f = f2, you make the pointers to reference the same objects. Also, if you have a function that accepts objects with an heritage hierarchy, the compiler knows at run time which object you're passing. Suppose for example, you have a class that represents a virtual world. And you have zillions of beings that are derived from a base class. Without polymorphism, If you were to implement a method that used any of these beings as parameters you would end up with an implementation for each. With polymorphism, this is not required. Suppose you have: Being->Warrior->Wizard->Preacher->Worker and any of these have a function named boolean Eat( ). Suppose in your code, there's an specific event where you need to pass an object and inside this method you make use of the Eat( ) function. Your implementation would be simple:

void IWilluseThatFunction( Being b){
if ( b.Eat( ) ) {
//.. do this
else {
//do that


I actually don't mind if Python does not allow it. I have implemented several simple non OOP functions in Python (simple enough, that everybody might have done it too) and it is too clear and direct. Suppose I would require a library with functions Jaca1, Jaca2...Jacan. In the OOP way, I would require an instantion of a class with the proper parameters and then I could make use of the functions. The non OOP way simple allows that I call every function passing the proper parameters. If I am just trying to get results from a data and I will not use them in another block of code, I'd not use OOP.

#OOP way
JacaObject = Jaca(HereIPassAXMLFileForEx)
#now I will have to access its methods
JacaObject.Jaca1( );

#Non OOP
from JacaCollectionOfFunctions import *


I did like the Tkinter GUI library that Python has built in. I found it faster than Swing. And both Swing and Tkinter make use of a similar OOP model.

Perhaps tempted, I would say that I still enjoy Java better, but Python comes close.

NP: DIO "Magica", excellent metal album!

No comments: