python terminology on classes

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Tue Jul 27 03:17:27 EDT 2010


Peng Yu a écrit :
> Hi
> 
> I'm still kind of confused about the terminology on classes in python.
> 
> Could you please let me know what the equivalent terms for the
> following C++ terms?

C++ and Python having very different semantics and object models, 
there's not necessarily a straight one to one mapping.

> 
> constructor

Python uses a smalltalk-like 2 phases instanciation / initialisation 
scheme. First the "proper" construction (__new__) is called with the 
class object as first argument, and it must return an unintialised 
instance of the class. Then the initialiser (__init__) is called on this 
instance.

Now one usually only implements the initialiser, as the default 
object.__new__ method does what you would expect, so you'll often see 
people qualifying __init__ as the constructor.

> destructor

Python has no real destructor. You can implement a __del__ method that 
will _eventually_ be called before the instance gets garbage-collected, 
but you'd rather not rely on it. Also, implementing this method will 
prevent cycle detection.

> member function

=> method.

Note that Python's "methods" are really thin wrappers around function 
objects that are attributes of the class object. You'll find more on 
this here:

http://wiki.python.org/moin/FromFunctionToMethod

> member variable

=> Attribute

> virtual member function

All Python's methods are virtual.

> function

=> function !-)

Note that in Python, functions and classes are objects.

> I think that C++ "function" is equivalent to python "function" and C++
> "member function" is equivalent to python "method". But I couldn't
> locate where the original definitions of the corresponding python
> terms in the manual as these term appear many times. Could you please
> point me where to look for the definition of these python
> corresponding terms?

You just cannot directly translate C++ into Python, and while there are 
similarities trying to write C++ in Python will not get you very far.




More information about the Python-list mailing list