[Tutor] Protected methods/variables

w chun wescpy at gmail.com
Wed Apr 5 12:46:47 CEST 2006


On 4/4/06, Kent Johnson <kent37 at tds.net> wrote:
> Mike Hansen wrote:
> >>> - you can use __slots__ to restrict arbirtrary creation of
> >> (dynamic)
> >>> instrance attributes
> >> You can do this, but it is generally considered a misuse of
> >> __slots__ and potentially problematic.

what specific problems were you thinking of?  i would say that's it's
clumsy to use during development because you're constantly changing
__dict__ by adding new instance attributes, etc., so i only add a
__slots__ when i'm finally done with the coding and want to prevent
others from creating (dynamically) more instance attributes.  it's too
bad, because it sort goes against Zen#19 (namespaces are a honking
good idea).


> > I'll bite. What is the proper/intended use of __slots__? Does it have
> > something to do with memory?
>
> Yes, it is intended specifically to reduce memory consumption of objects
> that are going to be instantiated a lot. I'm not sure how many counts as
> a lot, but in the thousands at least. Using __slots__ saves the cost of
> the attribute dict for each instance.

right, a class will either have __slots__ or __dict__ but not both.

-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com


More information about the Tutor mailing list