Re:PEP 435 Accepted – Adding an Enum type to the Python stan...
by saidajigumi on Friday May 10, 2013 @04:39PM
(#5688552)
Here's an example from the Python 3 docs[1] that specifically uses metaclass for exactly this scenario:
class OrderedClass(type):
@classmethod
def __prepare__(metacls, name, bases, **kwds):
return collections.OrderedDict()
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
>>> A.members
('__module__', 'one', 'two', 'three', 'four')
[1] http://docs.python.org/3/reference/datamodel.html#metaclass-...
Unless this is changed in Py 3, I still don't understand how this works.