__setattr__ recursion problem

Joonas Paalasmaa joonas at olen.to
Thu Nov 29 12:48:39 EST 2001


gbreed at cix.compulink.co.uk wrote:
> 
> 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

Thanks



More information about the Python-list mailing list