[Python-ideas] method decorators @final and @override in Python 2.4

Nick Coghlan ncoghlan at gmail.com
Sun Mar 29 03:09:44 CEST 2009


Péter Szabó wrote:
> (One easy way
> to do this right now is saying ``__metaclass__ = type'' in the
> subclass.)

Actually, that doesn't work as you might think...

>>> class TryNotToBeAnABC(FooABC):
...   __metaclass__ = type
...
>>> type(TryNotToBeAnABC)
<class 'abc.ABCMeta'>

The value assigned to '__metaclass__' (or the metaclass keyword argument
in Py3k) is only one candidate metaclass that the metaclass
determination algorithm considers - the metaclasses of all base classes
are also candidates, and the algorithm picks the one which is a subclass
of all of the candidate classes. If none of the candidates meet that
criteria, then it complains loudly:

>>> class OtherMeta(type): pass
...
>>> class TryNotToBeAnABC(FooABC):
...   __metaclass__ = OtherMeta
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
    metaclass conflict: the metaclass of a derived class must be a
(non-strict) subclass of the metaclasses of all its bases

And remember, as far as @overrides goes, I believe @abc.abstractmethod
already does what you want - it's only the
@suggest_final/@override_final part of the idea that doesn't exist.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------



More information about the Python-ideas mailing list