From: Steven D'Aprano on 26 Jul 2010 20:09 On Tue, 27 Jul 2010 01:15:08 +0200, Thomas Jollans wrote: > http://docs.python.org/py3k/reference/datamodel.html should answer all > your questions. It should, but as far as I can tell it doesn't. If it defines "attribute" or "method", I can't find it. -- Steven
From: Bruno Desthuilliers on 27 Jul 2010 03:17 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.
From: John Nagle on 27 Jul 2010 13:28 On 7/27/2010 12:17 AM, Bruno Desthuilliers wrote: >> 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. 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
From: Terry Reedy on 28 Jul 2010 18:16
On 7/27/2010 1:28 PM, John Nagle wrote: > 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. -- Terry Jan Reedy |