[Python-Dev] Requesting that a class be a new-style class
aleax at aleax.it
Sun Feb 20 09:15:25 CET 2005
On 2005 Feb 20, at 04:35, Jack Diederich wrote:
> I always use new style classes so I only have to remember one set of
I agree: that's reason #1 I recommend always using new-style whenever I
teach / tutor / mentor in Python nowadays.
> "__metaclass__ = type" is warty, it has the "action at a distance"
> problem that
> decorators solve for functions.
I disagree. I view it as akin to a "from __future__ import" except
that -- since the compiler doesn't need-to-know, as typeclass-picking
happens at runtime -- it was accomplished by less magical and more
> I didn't dig into the C but does having 'type'
> as metaclass guarantee the same behavior as inheriting 'object' or
> does object
> provide something type doesn't? *wince*
I believe the former holds, since for example:
>>> class X: __metaclass__ = type
If you're making a newstyle class with an oldstyle base, it's different:
>>> class Y: pass
>>> class X(Y): __metaclass__ = type
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: Error when calling the metaclass bases
a new-style class can't have only classic bases
in this case, you do need to inherit object explicitly:
>>> class X(Y, object): pass
(<class __main__.Y at 0x38d330>, <type 'object'>)
This is because types.ClassType turns somersaults to enable this: in
this latter construct, Python's mechanisms determine ClassType as the
metaclass (it's the metaclass of the first base class), but then
ClassType in turn sniffs around for another metaclass to delegate to,
among the supplied bases, and having found one washes its hands of the
More information about the Python-Dev