__setattr__ recursion problem
gbreed at cix.compulink.co.uk
gbreed at cix.compulink.co.uk
Thu Nov 29 12:31:02 EST 2001
In article <3C066BF8.8D911FCB at olen.to>, joonas at olen.to (Joonas Paalasmaa)
wrote:
> The code above causes an infinite loop. How can I set an attribute of
> Class
> without overloading the __setattr__ function? Or does someone have some
> other
> solution to make that script work?
>
>
>
> class Class2:
> eggs = 1
> spam = 2
>
> class Class:
> base = Class2()
> def __setattr__(s,attr, value):
> if hasattr(s.base, attr):
> setattr(s.base, attr, value)
> else:
> setattr(s, attr, value)
>
> Class().foo = "eggs"
Um, well, how about
class Class(Class2):
pass
?
or change
setattr(s, attr, value)
to
s.__dict__[attr]=value
Graham
More information about the Python-list
mailing list