__slots__
Ziga Seilnacht
ziga.seilnacht at gmail.com
Thu Mar 23 04:32:46 EST 2006
David Isaac wrote:
> 1. "Without a __dict__ variable,
> instances cannot be assigned new variables not listed in the __slots__
> definition."
>
> So this seemed an interesting restriction to impose in some instances,
> but I've noticed that this behavior is being called by some a side effect
> the reliance on which is considered unPythonic. Why?
If you want to restrict attribute asignment, you should use the
__setattr__ special method, see:
http://docs.python.org/ref/attribute-access.html
> 2. What is a simple example where use of slots has caused "subtle" problems,
> as some claim it will?
The first point is true only if all bases use __slots__:
>>> class A(object):
... pass
...
>>> class B(A):
... __slots__ = ('spam',)
...
>>> b = B()
>>> b.eggs = 1
>>> b.eggs
1
> 3. What is a simple example of a Pythonic use of __slots__ that does NOT
> involved the creation of **many** instances.
>
> Thanks,
> Alan Isaac
Ziga
More information about the Python-list
mailing list