Defining a metaclass
- class M(type):
- pass
- class A(metaclass=M):
- pass
In Python 2, the syntax for defining a metaclass is different:
- class A(object):
- __metaclass__ = M
Mypy also supports using six.with_metaclass()
and @six.add_metaclass
to define metaclass in a portable way:
- import six
- class A(six.with_metaclass(M)):
- pass
- @six.add_metaclass(M)
- class C(object):
- pass