Are there any other ways to iterate through the attributes of a custom class, excluding the in-built ones?
- by Ricardo Altamirano
Is there another way to iterate through only the attributes of a custom class that are not in-built (e.g. __dict__, __module__, etc.)? For example, in this code:
class Terrain:
WATER = -1
GRASS = 0
HILL = 1
MOUNTAIN = 2
I can iterate through all of these attributes like this:
for key, value in Terrain.__dict__.items():
print("{: <11}".format(key), " --> ", value)
which outputs:
MOUNTAIN --> 2
__module__ --> __main__
WATER --> -1
HILL --> 1
__dict__ --> <attribute '__dict__' of 'Terrain' objects>
GRASS --> 0
__weakref__ --> <attribute '__weakref__' of 'Terrain' objects>
__doc__ --> None
If I just want the integer arguments (a rudimentary version of an enumerated type), I can use this:
for key, value in Terrain.__dict__.items():
if type(value) is int: # type(value) == int
print("{: <11}".format(key), " --> ", value)
this gives the expected result:
MOUNTAIN --> 2
WATER --> -1
HILL --> 1
GRASS --> 0
Is it possible to iterate through only the non-in-built attributes of a custom class independent of type, e.g. if the attributes are not all integral. Presumably I could expand the conditional to include more types, but I want to know if there are other ways I'm missing.