Example
class A:
foo = 1
class B:
foo = 2
class C:
foo = 3
class D(A, B, C):
pass
def collect_foo(cls):
import inspect
foos = []
for c in inspect.getmro(cls):
if hasattr(c, 'foo'):
foos.append(c.foo)
return foos
Now collect_foo(D)
returns [1, 1, 2, 3]
- 1
is doubled as D
derives it from A
. The question is - how to get unique foo
s. First thing which came to my mind was checking if property is derived or declared in given class - is it possible? How to do that?
Just check
'foo' in c.__dict__
instead of
hasattr(c, 'foo')
This will only yield True
if the attribute is defined in c
itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With