python terminology on classes

P

Peng Yu

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?

constructor
destructor
member function
member variable
virtual member function
function

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?
 
S

Steven D'Aprano

Could you please let me know what the equivalent terms for the following
C++ terms?

constructor
destructor
member function
member variable
virtual member function
function


(1) Python new-style classes have a constructor __new__ and an
initialiser __init__. Some people describe both as constructors, but
that's strictly incorrect because the instance has already been
constructed by the time __init__ is called. (Old-style classes don't have
__new__, only __init__.)

(2) Python destructors are called __del__ , but you shouldn't use them
unless you really know what you are doing.

(3) "Member functions" are methods.

(4) "Member variables" are attributes. If you have to distinguish between
attributes which live on the instance from one that lives on the class,
"instance attribute" and "class attribute".

(5) I believe that all methods in Python are virtual.

(6) Function.

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?

I believe you are right, but I can't find a definition of C++ "member
function" that makes sense. Can you please point me where to look for the
definition of these C++ terms?

I don't believe the Python Language Reference explicitly defines terms
such as "attribute" and "method", but the tutorial may help:

http://docs.python.org/tutorial/classes.html

Quote:
In C++ terminology, all class members (including the data members)
are public, and all member functions are virtual.


Note: although the docs occasionally use the term "members" for
attributes, it is considered more standard to use "attribute" or "method"
unless discussing data types defined at the C layer.
 
B

Bruno Desthuilliers

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 !-)

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.
 
J

John Nagle

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.

That's not correct. The Python language reference is at
"http://docs.python.org/reference/datamodel.html". In CPython,
either __del__ will be called when the reference count goes to
zero, or it won't be called at all. The garbage collector that
backs up the reference counting system doesn't delete objects with
__del__ methods, because of the usual problems with deletion from
a garbage collector. The defined semantics are that loop-free
structures are deleted properly, but loops with one object that
has a __del__ hang around forever. You can use weak pointers to
avoid loops.

IronPython and ShedSkin are garbage-collected implementations which
have quite different __del__ semantics. That's considered non-standard.

In C++, the RAII approach is popular and recommended.
In C++, it's routine to create local objects which, when they go out
of scope, close a file, unlock a lock, or close a window.
It's also widely used in Python, but it's now somewhat deprecated.

Python 2.6 has a recently added "with" clause, borrowed from
LISP, for associating actions with scopes. This is supported for
files and locks, but setting your own object up for "with"
requires adding special methods to the object. "with" is less
convenient and more limited than RAII, but that's the direction
Python is going. This may be in preparation for a move to a real
garbage collector.

John Nagle
 
T

Terry Reedy

Python 2.6 has a recently added "with" clause, borrowed from
LISP, for associating actions with scopes. This is supported for
files and locks, but setting your own object up for "with"
requires adding special methods to the object. "with" is less
convenient and more limited than RAII, but that's the direction
Python is going. This may be in preparation for a move to a real
garbage collector.

I do not remember that being stated as part of the rationale for 'with',
but just today someone noted that since 'with' replace most uses of
deterministic refcount gc, there is not more scope for changing things,
at least for builtin and user classes, as opposed to C-extension classes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top