How do these Java concepts translate to Python?

S

Steven Bethard

bruno said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top