setattr getattr confusion

Donn Ingle donn.ingle at gmail.com
Sat Dec 8 06:06:16 EST 2007


Hi,
 Here's some code, it's broken:


class Key( object ):
    def __init__(self):
        self.props = KeyProps()
    def __getattr__(self, v):
        return getattr( self.props,v )
    def __setattr__(self,var,val):
        object.__setattr__(self.props,var,val)
        
class KeyProps(object):
    def __init__(self):
        self.x="NOT SET YET"
k1=Key()

It does not run because of the recursion that happens, but I don't know how
to lay this out.

I am trying to set the x value within props within Key:
k1.x="DAMN"
print k1.x

It seems to work, but it's really making a new 'x' in k1.
print k1.props.x
Shows "NOT SET YET", thus proving it failed to set x here.

I want to change k1.props.x by k1.x="something new" -- can this be done?

\d




More information about the Python-list mailing list