[Python-Dev] New metaclass pattern (Was Re: Simulating Class (was Re: Does Python have Class methods))
Thomas Heller
thomas.heller@ion-tof.com
Wed, 23 May 2001 20:57:28 +0200
Let me try again (and please forgive my
mistakes in the detail).
The usual way (as in demo\metaclasses):
class B_Meta:
....
B = B_Meta('B', (), {})
class C(B):
pass
B is an instance of the (meta)class B_Meta.
C is now another instance of the same (meta)class.
because B.__class__, which is the (meta)class itself,
is called, and returns a new instance.
B_Meta can (and must) implement a lot of behaviour.
In contrast, with my recipe:
def MagicFunction(name, bases, dict):
...construct a class on the fly...
...create an instance of this class...
return aninstance_of_a_class
def B_Meta(): pass
B_Meta.__class__ = MagicFunction
class C(B):
pass
Now C is an_instance_of_a_class (which is an instance
of a normal python class), and thus does inherit the
normal behaviour of Python classes.
Thomas
PS: I'm sure this all will be much better in descr-branch.
I've checked it out and am playing with it from time
to time, but most of the time I have to use released
Python versions.