Assignment not the same as defining?

Joshua Macy l0819m0v0smfm001 at sneakemail.com
Wed Oct 3 18:17:32 EDT 2001


Dale Strickland-Clark wrote:

> A solution I thought I had to using __setattr__ doesn't seem to work.
> 
> class wibble:
> 	def __init__(self):
> 		<init code>
> 		self.__setattr__ = self.set
> 
> 	def set(self, attr, value):
> 		<assignment code>
> 


   If I understand what you're trying to do, I think the standard 
workaround for this it to override __setattr__ the way you want, and 
directly muck with __dict__ in __init__, e.g.

 >>> class C:
...   def __init__(self, stuff):
...      self.__dict__['stuff'] = stuff
...   def __setattr__(self, attr, val):
...      pass
 >>> c = C('1')
 >>> c.stuff
'1'
 >>> c.stuff = 2
 >>> c.stuff
'1'

  Joshua









More information about the Python-list mailing list