Towards faster Python implementations - theory

Klaas mike.klaas at gmail.com
Wed May 9 19:08:36 EDT 2007


On May 9, 10:02 am, John Nagle <n... at animats.com> wrote:

>      One option might be a class "simpleobject", from which other classes
> can inherit.  ("object" would become a subclass of "simpleobject").
> "simpleobject" classes would have the following restrictions:
>
>         - New fields and functions cannot be introduced from outside
>         the class.  Every field and function name must explicitly appear
>         at least once in the class definition.  Subclassing is still
>         allowed.
>         - Unless the class itself uses "getattr" or "setattr" on itself,
>         no external code can do so.  This lets the compiler eliminate the
>         object's dictionary unless the class itself needs it.
>
> This lets the compiler see all the field names and assign them fixed slots
> in a fixed sized object representation.  Basically, this means simple objects
> have a C/C++ like internal representation, with the performance that comes
> with that representation.

Hey look, it already exists:

>>> class A(object):
...     __slots__ = 'a b c d'.split()

>>> a = A()
>>> a.e = 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'A' object has no attribute 'e'
>>> hasattr(a, '__dict__')
False

-Mike




More information about the Python-list mailing list