How do I get the definition order of class attributes in Python?

I want to define light-weight classes that are supposed to represent data structures. As is the case with many data structures, the order of the data is important. So if I go ahead and define this:

class User(DataStructure):
    username = StringValue()
    password = StringValue()
    age = IntegerValue()

I am implying that this is a data structure where a string with the username comes first, followed by a string with the password, and finally the age of the user as an integer.

If you're familiar with Python, you'll know that the above class, User, is an object inheriting from type. It will, just like most other objects in Python, have a __dict__. And here-in lies my problem. This __dict__ is a hash map, so the order of the class attributes in the __dict__ is in no way related to their order of definition.

Is there any way I can figure out the actual definition order? I'm asking here before I go with one of the less sane methods I can think of...

Oh and just to be clear, what I want is a way to get this out of the above definition: ['username', 'password', 'age']

6
задан Blixt 18 November 2010 в 23:31
поделиться