Class and instance attributes are inherently unordered because they are stored in a dictionary.
There are several options:
1) classes defined with __slots__ do not use a dictionary, and the space for attributes is pre-allocated. I assume that the attributes are therefore ordered, so itβs basically possible to get the data. However, there are problems using the slots, and this is not recommended.
2) add the list / tuple attribute to the class with attribute order.
3) use the collection.namedtuple object to store attributes. A named element is a tuple in which you can give names to elements, and is similar to structure C. This will only work with Python 2.6. Since tuples are immutable, you will not be able to change attributes after creation - this may or may not be a problem depending on the class.
Edit:
Reflection __slots__ will not work, since I think that they apply only to instances, and not to class attributes.
source share