Metaclasses broke in 2.2?
Guido van Rossum
guido at python.org
Tue Aug 14 11:42:15 EDT 2001
[Drew Csillag]
> > > I don't know if this was intended, but it appears that the metaclass
> > > instantiation doesn't work with Python 2.2a1. Are the old metaclass
> > > things no longer supported in 2.2 or is it just temporarily broken?
[Guido]
> > Can you be more specific? What old metaclass are you referring to?
> > It's probably broken because I changed the Don Beaudry hook invocation
> > to favor the new metaclass mechanism that is central to the new types
> > in 2.2.
[Drew]
> Take any of the code in Demo/metaclasses in 2.2a1. You get tracebacks
> of the variety:
>
> Traceback (most recent call last):
> File "Eiffel.py", line 113, in ?
> _test()
> File "Eiffel.py", line 101, in _test
> class C(Eiffel):
> TypeError: cannot create 'instance' instances
> drew:~/build/Python-2.2a1/Demo/metaclasses>../../python Enum.py
> Traceback (most recent call last):
> File "Enum.py", line 169, in ?
> _test()
> File "Enum.py", line 128, in _test
> class Color(Enum):
> TypeError: cannot create 'instance' instances
> drew:~/build/Python-2.2a1/Demo/metaclasses>
>
> I guess the real question is: "how does one go about writing metaclasses
> in Python code in 2.2?"
I see. You will be able to subclass from 'type' (or types.TypeType if
you prefer :-). Right now this doesn't work right (neiter in 2.2a1
nor in CVS) so I can't post a working demo :-(
But here's something that you *should* be able to write:
class mytype(type):
def __new__(*args):
print "mytype.__new__" + str(args)
return type.__new__(*args)
class C:
__metaclass__ = mytype
print C.__class__ # <type 'mytype'>
a = C()
print a.__class__ # <type 'C'>
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-list
mailing list