__slots__ vs __dict__

Andrew Bennetts andrew-pythonlist at puzzling.org
Thu May 13 01:12:44 EDT 2004


On Thu, May 13, 2004 at 06:55:39AM +0200, Marcus von Appen wrote:
> Andrew Bennetts <andrew-pythonlist at puzzling.org> writes:
> 
> [...]
> >> 
> >> # start
> >> class Foo (object):
> >>     __slots__ = "_test", "_test2"
> >>         
> >>     def __init__ (self):
> >>         self._test = 1
> >>         self._test2 = 2
> >>         
> >>     def __setattr__ (self, name,  value):
> >>         # just test a simple rebinding
> >>         self.__slots__ = self.__slots__ 
> >>         return
> >>         
> >> if __name__ == "__main__":
> >>     f = Foo ()
> >>     f.testvar = "test"
> >>     return
> >> # end 
> >
> > Also a syntax error.  I also don't get a bus error, just infinite recursion
> > (because assigning to self.__slots__ calls __setattr__, but __slots__ isn't
> > an attribute of the instance).  What version of Python, and what platform?
> 
> Typed in a wrong line again here. Change it to something appropriate like
> 
> def __setattr__ (self, name,  value):
>     # just test a simple rebinding
>     self.__slots__.__add__ (tuple (name))
>     return

Now your example runs just fine (i.e. the script terminates normally, and
nothing happens).  I still don't see any error.  (And why do you keep
putting a redundant return at the end of your functions?)

Are you sure this is the same code that you get a crash with?  What version
of Python are you using? (I've tested with 2.2.3 and 2.3.3).

-Andrew.





More information about the Python-list mailing list