__slots_ and inheritance
Alex Martelli
aleax at aleax.it
Mon Apr 14 15:55:46 EDT 2003
Andrew Dalke wrote:
> Martelli-bot:
>> Still, it DOES mention, as the
>> one and only claimed advantage, that, without __slots__, "using a
>> separate dictionary to hold a single instance variable doubles the
>> memory used", and then proceeds to list the resulting limitations
>> as "tidbits and warnings". Any different reading is clearly wishful
>> thinking, and the design intent seems pretty clear here.
>
> I was recently looking at a Python package ported from Java.
> It was the first package I've reviewed which used __slots__.
> My impression is that __slots__ were used as a rough approximation
> to declaring instance attributes, and not because of an explicit desire
> to save space or improve lookup performance. (That desire may
> be implicit, but given the early state of the code it would be
> premature to act on it.)
>
> Given the style of the code, it appears that the author is not an
> experienced programmer (eg, used "if flag is True" rather than
> "if flag"), which I believe back my belief that people from statically
> type languages look for a way to declare attributes, find __slots__,
> and use it, not really knowing the appropriateness of this approach.
Yes, I fear this experience may indeed be typical.
> This is different than the design intent. Should the documentation
> be updated to caution against using __slots__ except for cases
> which fit better the original intent? ....
I would vote for a big YES on that, if it were up for a vote. But
where does "the documentation" (reference manuals) cover __slots__?
>> > 1. what's the point of not automatically including a slot for
>> > '__weakref__'
>> > (hardly the unreasonable memory consumption such a slot would
> impose?)?
>>
>> Indeed definitely that -- increasing memory consumption by e.g. 50%
>> for an object with two "true" slots, just in case somebody in the
>> future MAY want to weakreference it, would be a weird choice. If
>> you need your instances to be weakly referenceable include the slot
>> explicitly and pay the attendand memory price.
>
> I don't know __slots__ well enough. Does you mean that if I want
> weak references for these I can simply add that slot, as in
>
> __slots__ = [..., "__weakref__"]
> ?
Haven't seen it documented anywhere, actually. But, e.g....:
>>> class W(object):
... __slots__ = ['x', '__weakref__']
... def __init__(self, x): self.x = x
... def __repr__(self): return 'W(%d)'%self.x
...
>>> wlst = [W(i) for i in range(11, 33)]
>>> wd = weakref.WeakValueDictionary(zip(range(len(wlst)),wlst))
>>> for w in wlst[:]:
... if w.x%5: wlst.remove(w)
... del w
...
>>> wlst
[W(15), W(20), W(25), W(30)]
>>> wd.values()
[W(15), W(20), W(25), W(30)]
>>>
seems to work just as well as it would without __slots__.
>> Sure. So what? A trickily coded class could always throw introspection
>> based on instance __dict__ off -- __getattr__ could and often did "add"
>> attributes not otherwise visible.
>
> Though it should have also implemented __methods__ and/or __members__,
> to return the list of otherwise invisible fields.
>
> Very few __getattr__ implementation did this. It's documented, but hard
> to tell when/why it's useful.
Indeed -- I don't think I ever remembered to do anything like that,
personally...;-).
Alex
More information about the Python-list
mailing list