Python comes with the handy dir()
function that would list the content of a class for you. For example, for this class:
class C:
i = 1
a = 'b'
dir(C)
would return
['__doc__', '__module__', 'a', 'i']
This is great, but notice how the order of 'a'
and 'i'
is now different then the order they were defined in.
How can I iterate over the attributes of C (potentially ignoring the built-in doc & module attributes) in the order they were defined? For the C class above, the would be 'i'
then 'a'
.
Addendum: - I'm working on some serialization/logging code in which I want to serialize attributes in the order they were defined so that the output would be similar to the code which created the class.