5 ms·
There's no need to modify the interpreter. Redefining __dict__ to support ordering would be enough, or perhaps defining a purpose-built __metaclass__[1]. [1]
by saidajigumi 13y ago
There's no need to modify the interpreter. Redefining __dict__ to support ordering would be enough, or perhaps defining a purpose-built __metaclass__[1].
[1] http://stackoverflow.com/a/6581949/183481 http://stackoverflow.com/a/6581949/183481
- marmaduke 13y agoNo-go on metaclass, the received type for metaclass __new__ method's members argument is a plain dict. Unless this is changed in Py 3, I still don't understand how this works.
- saidajigumi 13y agoHere'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-example http://docs.python.org/3/reference/datamodel.html#metaclass-...