使用元类

元类语法

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