[Python-3000] Compiling the PEP 3115 metaclass syntax
Guido van Rossum
guido at python.org
Thu Mar 15 02:26:14 CET 2007
The PEP proposes that the class statement accepts keyword arguments,
*args, and **kwds syntax as well as positional bases. This is a bit
messy to compile and execute, but we already have this, of course, in
the code for calling regular functions.
So I think it would be acceptable to this into a call to a new
(hidden) built-in function, named __build_class__. Then that this
class definition:
class C(A, B, metaclass=M, other=42, *more_bases, *more_kwds):
...
would translate into this:
C = __build_class__(<func>, 'C', A, B, metaclass=M, other=42,
*more_bases, *more_kwds)
where <func> is a function object for the class body. (It's a slightly
different function than currently; the current function *returns* the
locals, while the new one *takes* the locals as an argument; instead
of a LOAD_LOCALS opcode we need a STORE_LOCALS opcode.)
Then __build_class__ could be roughly like this (but implemented in C):
def __build_class__(func, name, *bases, metaclass=None, **kwds):
if metaclass is None:
metaclass = extract_metaclass(bases) # may raise an exception
prepare = getattr(metaclass, "__prepare__", None)
if prepare:
locals = prepare(name, bases, **kwds)
else:
locals = {}
func(locals)
return metaclass(name, bases, locals, **kwds)
What do folks think?
--
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-3000
mailing list