Why self?
Fernando PĂ©rez
fperez528 at yahoo.com
Thu Jul 11 19:30:01 EDT 2002
- c o v e n t r y - wrote:
> No, you are only using a convention to let other developers know
> that these variables are private - the python interpreter does not
> even attempt to hide a '__' variable from other classes or code.
> Private members are by convention only in python, they are not
> enforced, thus you do not realistically get a similar result.
>
wrong. The interpreter mangles the __names automagically by adding the class
name. You can work around it, but at least it helps to prevent accidents. So
it is true that it is only a convention, but the interpreter does go to
reasonable lengths to enforce it:
In [14]: class foo:
....: def __init__(self):
....: self.__priv = 1
....:
In [15]: x=foo()
In [16]: x.__priv
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
?
AttributeError: foo instance has no attribute '__priv'
In [17]: x._foo__priv
Out[17]: 1
cheers,
f.
More information about the Python-list
mailing list