__slots__
David Isaac
aisaac0 at verizon.net
Thu Mar 23 23:34:32 EST 2006
"Ziga Seilnacht" <ziga.seilnacht at gmail.com> wrote:
> If you want to restrict attribute asignment, you should use the
> __setattr__ special method, see:
> http://docs.python.org/ref/attribute-access.html
That "should" is what I am asking about. If I understand,
in the simplest case, you want me to say something like
def __setattr__(self,name,value):
if name in myattrlist:
object.__setattr__(self,name,value)
else:
raise AttributeError
instead just saying
__slots__ = myattrlist
I understand that this *is* the prevailing advice. But why?
> >>> class A(object):
> ... pass
> ...
> >>> class B(A):
> ... __slots__ = ('spam',)
> ...
> >>> b = B()
> >>> b.eggs = 1
> >>> b.eggs
> 1
A good example of something that could be easily
missed, and possibly an answer to my question above.
(Although not I think if I am subclassing object.)
Thanks,
Alan Isaac
More information about the Python-list
mailing list