__slots__ and multiple inheritance
Bengt Richter
bokr at oz.net
Sun Dec 15 12:07:36 EST 2002
On Sat, 14 Dec 2002 20:19:40 -0500, Carl Banks <imbosol at vt.edu> wrote:
>Bengt Richter wrote:
>> ISTM __xxx__ primarily serves as an object definition metalanguage (1).
>> I.e., we use it to compose objects with standard behaviours and attributes
>> used implicitly in higher level constructs. Perhaps moving towards using
>> __xxx__ purely for that primary purpose would be a good thing?
>>
>> 1. standard primitive object attributes
>> 2. compiler hints/directives
>> 2.1 allocation-related
>> 2.2 metaclass linkage
>> 3. import/loader hints/directives
>> 4. ?
>
>Personally, I don't have any problem with __xxx__ meaning simply "this
>symbol has extra or different meaning given to it by the language."
Me neither (1. above), but I was talking about whether some __xxx__ codes are
really impostors and not part of the programming language *itself* at all,
any more than -OO is part of the language *per se*.
>Trying to codify and restrict what the meanings might be is just being
>too anal retentive about it.
Cleanliness is next to goodliness ;-)
>
>
>> I.e. __slots__ seems effectively an optimization hint (actually
>> directive, I guess). Or is the restriction above a design intent
>> rather than a necessary side effect?
>
>No, it's definitely a necessary side effect. If they could have done
>it without the MI restriction, they would have. And it's definitely
>not an optimization. Its purpose is to help avoid misspelling bugs.
Excerpt from http://www.python.org/2.2/descrintro.html:
""""
...
This is not always what you want; in particular, using a separate dictionary to
hold a single instance variable doubles the memory used by a defaultdict
instance compared to using a regular dictionary! There's a way to avoid this:
class defaultdict2(dict):
__slots__ = ['default']
def __init__(self, default=None):
...(like before)...
The __slots__ declaration takes a list of instance variables, and reserves space
in the instance for exactly these in the instance. When __slots__ is used, other
instance variables cannot be assigned to:
...
"""
which sounds to me like space optimization is at least a desired side effect.
>
>Personally, I never use slots, because I've never found misspelling
>bugs to be much of a problem.
>
If you don't use a tool to help check set-but-never-used in a large program,
the probability of a silent binding to a misspelled name is nonzero ;-)
And if it happened on exit from a loop where you were binding the last value
of a converging approximation algorithm, the chances of your noticing that
it didn't "take" is low, unless you are "anal" about testing ;-)
Regards,
Bengt Richter
More information about the Python-list
mailing list