__setattr__ recursion problem

Oleg Broytmann phd at phd.pp.ru
Thu Nov 29 12:27:13 EST 2001


On Thu, Nov 29, 2001 at 07:10:16PM +0200, 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)

   To avoid recursion the last line must be

 			s.__dict__[attr] = value

> Class().foo = "eggs"

Oleg.
-- 
     Oleg Broytmann            http://phd.pp.ru/            phd at phd.pp.ru
           Programmers don't die, they just GOSUB without RETURN.




More information about the Python-list mailing list