__slots__

john coppola john_coppola_r_s at yahoo.com
Wed Feb 13 14:16:13 EST 2002


Hello Python developers!  I have been reading about
Python 2.2's new features and I have to say there's
lots of good stuff going on. The feature I dedicate
this message to is slots.  This ubiquitous new feature
has the potential to boost performance of python quite
considerable and it is the first time python has ever
hinted at, for lack of a better word, "being static".

I used this word loosely.  What I mean by this is that
typically one could set attributes arbitrarily on
python objects, but now cometh slots, if __slots__ is
defined, one could set only attribute names defined in
slots if __slots__ exists.

In introductory essay on the new python object system,
"Unifying types and classes in 2_2", I found Guido's
answer for the existence of __slots__ unsatisfactory. 
As implied from the introductory essay, __slots__ was
used as a means to eliminate a double dictionary for
the dictionary subclass in the
example.  That is one application of slots, but I
believe there is a more powerful way to use this new
feature.

First let me discuss some notational issues I have
with __slots__.  It is possible to use any object that
supports iteration as the __slots__ object. I feel
that this is not a good idea.  Consider the following
example:

    class Foo(object):
        __slots__=['a','b','c']

    foo=Foo()

Bad, but you could do it.

    foo.__class__.__slots__.append('d')

Suppose someone wrote code to initialize some instance
like so:

    for attr in foo.__class__.__slots__:
        setattr(foo,attr,0)

Granted this is decidedly bad code but the problem
this creates is that attribute "d" does not exist in
foo or any instance of Foo.  So an attribute
error will be raised. Slot attributes are read only
thus del foo.__slots__ is not possible.  So neither
should __slots__.append be legal.  So the answer
is to enforce __slots__ is tuple, or for a single
attribute __slots__ is string, when generating code
objects.

Another issue I have with __slots__ is that there
exists better notation to do the same thing.  Why not
introduce new a keyword into the Python language so
that "static attribute sets" are created like so:

    class Foo(object):
        attribute a
        attribute b
        attribute c

Perhaps this syntax could be expanded further to
include strict typing:

    class Foo(object):
        attribute a is str
        attribute b is float
        attribute c is int

I feel that this notation is much more lucid and could
lead to even more interesting notation:

    class Foo(object):
	attribute a is str
	attribute b is float
	attribute c is property
	attribute g is property of float

I hope that it is understood from this context that
"is" compares the type(<attrname>) to trailing type in
the declaration.  The meaning of the modifier "of" in
the last line is a bit ambiguous at this moment. 
Perhaps it enforces the type passed into fset, and the
return value of fget.


We have for the first time static object attributes in
Python that prior to 2.2 never existed.  Is the python
interpreter  __slot__ aware?  Instead of the usual
variable name lookup into __dict__ (which is not
supported by instances who's class implements
__slots__), a slot attribute could be directly
referenced via pointer indirection.   Does the
interpreter substitute references for slot
attributes?  If my question is not clear let me
further elaborate:

    foo.a = "Some value"
    foo.b = 10.0
    foo.c = "spam"

is really   (in pseudo-code)

    Py_XDECREF(self -> some_slot_offset + 0)
    (foo -> some_slot_offset + 0) = <"Some value">
    Py_XDECREF(self -> some_slot_offset + 1)
    (foo -> some_slot_offset + 1) = <10.0>
    Py_XDECREF(self -> some_slot_offset + 2)
    (foo -> some_slot_offset + 2) = <???>
    (don't know how to deal with properties yet)

Does the interpreter substitute "references" instead
of a naming lookups in the case that instance foo
supports slots?  For that matter any namespace
could utilize this new approach, even the local
namespace of a new code block. Slots could be built
implicitly for a local namespace.  This could lead to
significant performance boost where typically, speed
is order O(N), slots comparatively could allow order
O(N) divided by 10.


Please send your comments.  If anything is ambiguous
(and i'm sure there's plenty), let me know and I will
try to clarify what I am talking about.  If anyone
feels that this could be a PEP candidate that would be
cool.

Sincerely,
John Coppola
john_coppola_r_s at yahoo.com



__________________________________________________
Do You Yahoo!?
Send FREE Valentine eCards with Yahoo! Greetings!
http://greetings.yahoo.com




More information about the Python-list mailing list