[Python-3000] Changing order of class creation

Ian Bicking ianb at colorstudy.com
Mon Apr 24 17:53:39 CEST 2006


Here's something for the crazy idea bin...

Right now class creation looks like:

   class X(base):
       y = 'z'

This gets the things in the () (object), evaluates the body of the class 
in a new scope and captures that scope (namespace), and looks for a 
metaclass using a couple rules.  Then it does:

   X = metaclass('X', (base,), {'y': 'z'})

What if instead it did:

   X = metaclass('X', (base,))
   X.y = 'z'

?  X could keep track of order if it wanted.  Or not.  There would be no 
conflict between metaclasses paying special attention to the constructor 
namespace, and later attribute setting on the same class (most 
metaprogramming techniques with metaclasses choose one or the other). 
It could make rebuilding a class easier (as in reload).  If 
metaclass('X', (bases,)) returned the already-existant X class, 
attributes will get folded in.  (There's still problems there, but maybe 
this helps a little.)

I haven't really thought through all the effects this might have.  It 
does mean the underlying mechanism runs in the same order as the source, 
instead of the inverted order we currently have (where the body is 
evaluated before the class is created).

-- 
Ian Bicking  /  ianb at colorstudy.com  /  http://blog.ianbicking.org


More information about the Python-3000 mailing list