eval of a private attribute
J.Jacob
joost_jacob at hotmail.com
Thu Mar 21 11:14:13 EST 2002
How to get to a variable hidden behind two underscores...
The get() method in this class returns a public or 'private' attribute
or None if no such attribute.
class CObsFile:
def __init__(self):
self.__user = "~briner/big/usr.rdb"
self.public_user = "thatsme_public"
def get(self, var):
if hasattr(self, var):
tobeEval = var
p = getattr(self, var)
else:
try:
tobeEval = 'self._CObsFile__'+var
p = eval(tobeEval)
except AttributeError:
p = None
return p
p = CObsFile()
print p.get('public_user') # get a public attribute
print p.get('user') # get a 'private' attribute
print p.get('nosuchuser') # get a nonexisting attribute
More information about the Python-list
mailing list