Data attributes...

Emile van Sebille emile at fenx.com
Thu Jan 24 22:32:07 CET 2002


Mark McEahern wrote:
> Is this what __slots__ is for in new-style classes?  (Python 2.2).
>

Yes, but to get it to work it needs to be a new style class, ie subclass
from object.

This enforces attributes in __slots__ :

>>> class Test1(object):
...     __slots__ = ['a','b','c']
...
>>> t = Test1()
>>> t.d = 2
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'Test1' object has no attribute 'd'

but this doesn't:

>>> class Test2:
...     __slots__ = ['a','b','c']
...
>>> t = Test2()
>>> t.d = 2
>>>



--
Emile van Sebille
emile at fenx.com




More information about the Python-list mailing list