eval of a private attribute

Bertrand Geston bergeston at yahoo.fr
Thu Mar 21 06:24:35 EST 2002


Those work both:
--1. Instance variables------
class CObsFile:
    def __init__(self):
        self.__user="~briner/big/usr.rdb"
        self.__location="~genevay/system/location"
    def get(self,var):
        tobeEval ='self._CObsFile__'+var
        print 'toBeval: '+tobeEval
        p=eval(tobeEval)
        print 'Evaluated: '+p
        return p

p=CObsFile()
p.get('user')

--2. Class variables------
class CObsFile:
    __user="~briner/big/usr.rdb"
    __location="~genevay/system/location"
    def get(self,var):
        tobeEval = 'CObsFile._CObsFile__' + var
        print 'toBeval: '+tobeEval
        p=eval(tobeEval)
        print 'Evaluated: '+p
        return p

p=CObsFile()
p.get('user')
--------------------------

HTH

B.
"BRINER Cedric" <work at infomaniak.ch> wrote in message
news:3C998F89.66DB1290 at infomaniak.ch...
> hi,
> I'd like to create a method which will be able to get
> any attribute of the class.
>
> Unfortunately this example is not working:
>
>
> class CObsFile:
>   __user="~briner/big/usr.rdb"
>   __location="~genevay/system/location"
>   def get(self,var):
>     tobeEval ='self.__'+var
>     print 'toBeval: '+tobeEval
>     p=eval(tobeEval)
>     print 'Evaluated: '+p
>     return p
>
> p=CObsFile()
> p.get('user')
>
> any idea why





More information about the Python-list mailing list