Benjamin Kaplan wrote:
On Wed, Jul 29, 2009 at 1:59 PM, dandi kain <dandi.k...@gmail.com> wrote:
Hello everybody,
I have just started learning Python.I heard its simple so I pick a
presentation [1] and tried to work on it.But when it comes to
underscores leading and trailing an object I dont understand any.I
look through the python manual also but that was not helping .I
searched some forums and I still dont have a clear picture.
What is the functionality of __ or _ , leading or trailing an object ,
class ot function ? Is it just a naming convention to note special
functions and objects , or it really mean someting to Python ?
It's just a convention for the most part. A single leading underscore
is used for "private" attributes. Two leading underscores will affect
the code- it mangles the variable name so that you don't have to worry
about the value being overwritten by a subclass. For instance
"""
class Foo(object) :
def __init__(self) :
self.__bar = ''
foo = Foo()
""
will store the attribute as foo._Foo__bar.
Also, the "magic methods"- the ones that are used for operations and
built-in stuff, all have two leading and two trailing underscores.
These are things like __add__ (+), __eq__ (=), __cmp__ (old way for
comparisons), __len__ (len), __str__ (str), and so on.
For this last, see
http://docs.python.org/dev/py3k/reference/datamodel.html#special-method-names
--
http://mail.python.org/mailman/listinfo/python-list