metaclass exampleclass OrderedClass(type):
#该方法返回值就是__new__的namespace参数,如果没有该方法namespace的值就是dict()
@classmethod
def __prepare__(metacls, name, bases, **kwds):
return collections.OrderedDict()
#namespace就是class的__dict__,这个dict类型的对象已经被填充了相应的值
def __new__(cls, name, bases, namespace, **kwds):
result = type.__new__(cls, name, bases, dict(namespace))
result.members = tuple(namespace)
return result
class A(metaclass=OrderedClass):
def one(self): pass
def two(self): pass
def three(self): pass
def four(self): pass
print(A.members)
#('__module__', '__qualname__', 'one', 'two', 'three', 'four')