array of class / code optimization

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Thu Jan 4 05:44:16 EST 2007


mm a écrit :
> 
> Yes, it was the (), equivalent to thiks like new() create new object 
> from class xy.

Yeps. In Python there's no 'new' operator. Instead, classes are 
themselves 'callable' objects, acting as instance factory. It's very 
handy since it let's you replace a class with a factory function (or 
vice-versa) without impacting client code.

>>   s1.append(Word)
> 
> s1.append(Word())
> 
> But I was looking for a "struct" equivalent like in c/c++.

You can use a dict or a class.

> And/or "union". I can't find it.

What is your real use case ? Direct translation from one language to 
another usually leads to code that's both non-idiomatic and inefficient. 
IOW, don't try to write C++ in Python.

> Maybe you know a source (URL) "Python for c/c++ programmers" or things 
> like that.

Not really, but quite a lot of Python programmers come from the C++ 
world. FWIW, I'd say that the official Python tutorial should be enough 
to get you started. Then, and if you're at ease with OO, you may want to 
have a look at more advanced stuff like special methods and attributes, 
decriptors and metaclasses (here again documented on python.org).



More information about the Python-list mailing list