1

I'm trying to get all the class level attributes on a python instance, but when I access the class dictionary (inst.__class__.__dict__), I won't get the class level attributes from the parent type ex:

class Parent:
    var1 = ''

class Child(Parent):
    var2 = ''

child_inst = Child()
for key in child_inst.__class__.__dict__:
    print(key)

output: 'var2' (ignoring __atr__ attributes)

I can achieve what I want by doing a second loop through

for key in child_inst.__class__.__base__.__dict__:
    print(key)

output: 'var1'

but for a complex inheritance hierarchy I'd need to do this an unknown number of times. is there a simpler way to do this?

Edit:

I need to get unresolved properties for what I'm attempting to do, not the attribute values themselves I tried using inspect.getmembers, however that appears to resolve the properties

Cethyrion
  • 31
  • 4
  • 1
    Is there a reason you need to find these attributes by direct inspection of the object, rather than defining them as a list or something up front? Because it seems like you're just making work for yourself. Perhaps this is an [XY problem](https://meta.stackexchange.com/q/66377/223254) and there's a better solution to the larger design problem? – Blckknght Oct 12 '19 at 14:32
  • I'm actually using it as a shortcut to write less code, I'm working on a personal project where I'm defining many different types with property attributes, it's a little difficult to explain, and maybe I'm over-complicating it for myself, but basically all the base classes in my project inherit from a common type that finds and auto initializes the properties based on the data passed in – Cethyrion Oct 12 '19 at 14:49
  • If you would like I can add some code to show my use case – Cethyrion Oct 12 '19 at 14:50

0 Answers0