运行这个(Python 3.3.1):
from collections import OrderedDict
class MyType(type):
@classmethod
def __prepare__(*args):
return OrderedDict()
class MyClass2(metaclass=MyType):
attr1 = 1
attr2 = 2
attr3 = 3
attr4 = 4
attr5 = 5
def __init__(self):
self.attr6 = 6
def func(self):
pass
print(MyClass2.__dict__.items())
我正进入(状态:
dict_items([('__weakref__', <attribute '__weakref__' of 'MyClass2' objects>), ('__dict__', <attribute '__dict__' of 'MyClass2' objects>), ('__init__', <function MyClass2.__init__ at 0x7f08a106dc20>), ('__doc__', None), ('attr4', 4), ('attr5', 5), ('attr2', 2), ('attr3', 3), ('attr1', 1), ('func', <function MyClass2.func at 0x7f089f995c20>), ('__module__', '__main__')])
类属性不按其定义顺序排序。
我在OrderedDict
课堂上做错了__dict__
什么?