__slots__ vs __dict__

Marcus von Appen mva at sysfault.org
Thu May 13 00:55:39 EDT 2004


Andrew Bennetts <andrew-pythonlist at puzzling.org> writes:

[...]
>> # start
>> class Foo (object):
>>     __slots__ = "_test"
>>         
>>     def __init__ (self):
>>         self._test = None
>> 
>> if __name__ == "__main__":
>>     f = Foo ()
>>     f.testvar = "test"
>>     return
>> # end 
>
> This code is a syntax error for me -- the final return isn't in a function.

Sorry for that messy code - the return is absolutely wrong (no idea, why I put 
it there after typing the example in).

>> Rebinding __slots__ in __setattr__ fails with a bus error on my system (it 
>> should not be possible anyways, because __slots__ is a tuple):
>> 
>> # 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

> I'm guessing you're on something like FreeBSD where Python has had trouble
> coping gracefully with infinite recursion.

Right, but this was caused by typing in wrong code. Sorry for that.

Regards
Marcus

-- 
We don't understand the software, and sometimes we don't understand the 
hardware, but we can *see* the blinking lights!



More information about the Python-list mailing list