New-style classes (was Re: Python 2.2 properties)
Alex Martelli
aleax at aleax.it
Sun Jan 20 11:20:04 EST 2002
Stefan Schwarzer wrote:
> I just wondered if there is something like
>
> from __future__ import new_style_classes
>
> to get the derivation from object by default if no base class is
> specified.
AFAIK, setting the module-global __metaclass__ is the way to
do that. However, not the way you do it:
>>> __metaclass__ = object
This doesn't work because object is not designed as a metaclass:
>>> __metaclass__ = object
>>> class X: pass
...
>>> x=X()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: object is not callable: <object object at 0x816eae0>
>>>
Rather:
>>> __metaclass__ = type
>>> class X: pass
...
>>> x=X()
>>>
type is the metaclass you want to use here (and you can set the
module-global __metaclass__ back to types.ClassType to go back
to having classic classes by default).
Alex
More information about the Python-list
mailing list