Data attributes...

Alex Martelli aleax at aleax.it
Fri Jan 25 07:28:54 EST 2002


"Mark McEahern" <marklists at mceahern.com> wrote in message
news:mailman.1011898958.2676.python-list at python.org...
> Emile van Sebille wrote:
> > You can have the class enforce rules that only allow pre-defined
> > attributes.  Look for __setattr__ as you continue through the tutorial.
>
> Is this what __slots__ is for in new-style classes?  (Python 2.2).

__slots__ (only for new-style classes, i.e. classes inheriting
directly or indirectly from object, i.e. classes whose metaclass
is type) constrains what data attributes can be bound in instances,
and (more relevantly) optimizes memory consumption by doing away
with the __dict__ that otherwise each instance carries.

__setattr__ is more flexible, trickier to use, and definitely
not "an optimization" (on the contrary it places some overhead
on each attribute setting you do on the instance).


Alex






More information about the Python-list mailing list