According to Python 2.7.12 documentation, 3.4.3. Customizing class creation:
__metaclass__This variable can be any callable accepting arguments for name, bases, and dict. Upon class creation, the callable is used instead of the built-intype().New in version 2.2.
However, this article argues:
Q: Wow! Can I use any type object as the
__metaclass__?A: No. It must be a subclass of the type of the base object. ...
So I did an experiment on my own:
class metacls(list): # <--- subclassing list, rather than type
def __new__(mcs, name, bases, dict):
dict['foo'] = 'metacls was here'
return type.__new__(mcs, name, bases, dict)
class cls(object):
__metaclass__ = metacls
pass
This gives me:
Traceback (most recent call last):
File "test.py", line 6, in
class cls(object):
File "test.py", line 4, in __new__
return type.__new__(mcs, name, bases, dict)
TypeError: Error when calling the metaclass bases
type.__new__(metacls): metacls is not a subtype of type
So is the document really wrong?