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?
--
Regards,
Peng
constructor.
This consists of the class constructor method, __new__, and of the
instance initialization method, __init__
In practice, __init__ is really "the constructor".
http://docs.python.org/py3k/reference/datamodel.html#object.__new__
> destructor
destructor.
http://docs.python.org/py3k/reference/datamodel.html#object.__del__
> member function
method.
Look for "instance method" below
<URL:http://docs.python.org/py3k/reference/datamodel.html#the-standard-type-hierarchy>
> member variable
attribute, instance attribute, instance variable.
> virtual member function
all methods are virtual.
> 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?
http://docs.python.org/py3k/reference/datamodel.html should answer all
your questions.
> 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?
Seriously, we can't keep doing your thinking for you. The answers
to all your questions are section 9 of the tutorial.
--
Rhodri James *-* Wildebeeste Herder to the Masses
One small caveat -- IIRC, in Java/C++ the destructor is
guaranteed to be called with a certain amount of context. I find
Python's __del__ almost useless since things it may rely upon can
arbitrarily be destroyed before the __del__ is called.
-tkc
> 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.
--
Steven
> 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
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.
>> 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
> 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
This is is just the kind of newbie-hostile smart-ass reply that we do
not want to see on comp.lang.python.
Let's try again:
I think that the answers to all your questions are section 9 of the
tutorial.
http://docs.python.org/py3k/tutorial/index.html
Why don't you take a look at it, and then come back again if you still
have questions.
With Peng Yu, we've been through that quite a lot. It seemed time to
be a little sharper in the hope that learning might emerge.
--
Rhodri James *-* Wildebeest Herder to the Masses
> On Wed, 04 Aug 2010 19:28:48 +0100, Steve Ferg
> <steve.ferg...@gmail.com> wrote:
>
> >> Seriously, we can't keep doing your thinking for you. The answers
> >> to all your questions are section 9 of the tutorial.
> >
> > This is is just the kind of newbie-hostile smart-ass reply that we do
> > not want to see on comp.lang.python.
[…]
> With Peng Yu, we've been through that quite a lot. It seemed time to
> be a little sharper in the hope that learning might emerge.
As someone who generally deplores sharp replies to newbies for the
negative effect on the community as a whole and on later newcomers in
particular: I have to agree with Rhodri here. This specific case has
reached a point where some sharpness is warranted, in my opinion.
Peng Yu, please take Rhodri's reply in a spirit of mentoring. You have
all the tools at your disposal and they have been pointed out to you
numerous times. When asking a question of others, please demonstrate
that you have exhausted the existing resources you clearly know you have
available to you.
--
\ “What we usually pray to God is not that His will be done, but |
`\ that He approve ours.” —Helga Bergold Gross |
_o__) |
Ben Finney