The __prepare__
method in a mataclass allows you to provide your own custom object in place of the dict
used for class initialisation. However you cannot change the type of object actually used by the class, that will be an instance of an internal type known as mappingproxy
and is unordered. If you want to preserve the order in which the attributes where defined you must store that separately:
class OrderedClass(type):
@classmethod
def __prepare__(metacls, name, bases):
return OrderedDict()
def __new__(cls, name, bases, classdict):
result = type.__new__(cls, name, bases, classdict)
result.member_names = list(classdict.keys())
return result
class MyClass2(metaclass=OrderedClass):
attr1 = 1
attr2 = 2
attr3 = 3
attr4 = 4
attr5 = 5
def __init__(self):
self.attr6 = 6
def func(self):
pass
>>> MyClass2.member_names
['__module__', '__qualname__', 'attr1', 'attr2', 'attr3', 'attr4', 'attr5', '__init__', 'func']