使用元類
元類語法
Python 2.x <= 2.7
class MyClass(object):
__metaclass__ = SomeMetaclass
Python 3.x >= 3.0
class MyClass(metaclass=SomeMetaclass):
pass
Python 2 和 3 與 six
的相容性
import six
class MyClass(six.with_metaclass(SomeMetaclass)):
pass