How do these Java concepts translate to Python?
Steven Bethard
steven.bethard at gmail.com
Fri Aug 12 13:19:39 EDT 2005
bruno modulix wrote:
>>but technically
>>speaking, there are no public, protected, or private things.
>
> Yes there are:
> object.name is public
> object._name is protected
> object.__name is private
The double-underscore name-mangling is almost never worth it. It's
supposed to stop name collisions, but, while it does in some cases, it
doesn't in all cases, so you shouldn't rely on this. For example:
---------- mod1.py ----------
class C(object):
__x = 'mod1.C'
@classmethod
def getx(cls):
return cls.__x
-----------------------------
---------- mod2.py ----------
import mod1
class C(mod1.C):
__x = 'mod2.C'
-----------------------------
py> import mod1, mod2
py> mod1.C.getx()
'mod1.C'
py> mod2.C.getx()
'mod2.C'
If double-underscore name-mangling worked like private variables,
setting C.__x in mod2.C should not affect the value of C.__x in mod1.C.
STeVe
More information about the Python-list
mailing list