[Python-3000] PEP for Metaclasses in Python 3000

Greg Ewing greg.ewing at canterbury.ac.nz
Sat Mar 10 08:45:30 CET 2007


Jack Diederich wrote:

> I am a very big fan of ordered dicts in classes.  One possibility is that
> suites in classes always store their order in a special dict that keeps a 
> side list of key order.  A final invisible class decorator around
> every class would then toss out the order and leave only a regular dict.

Is it really necessary to toss out the order at all?
I'm skeptical that class dictionaries are either created
or modified anywhere near often enough for there to
be any noticeable performance penalty here.

In any case, I've thought of a way to reduce the performance
penalty to near-zero. Consider that the compiler knows
all the names that will be assigned in the class dict
and what order they are in. So all it needs to do is
compile

   class Foo:

     a = 5

     def b(self):
       ...

     def c(self, x):
       ...

as though it were

   class Foo:

     __names__ = ('a', 'b', 'c')

     a = 5

     def b(self):
       ...

     def c(self, x):
       ...

The tuple can be a constant of the code object, so the
only overhead is one extra item added to the dict at
class creation time, and none at all for subsequent
access to the class dict. And you don't even need a
special kind of dict.

--
Greg


More information about the Python-3000 mailing list