i am working on a project with long hierarchy of classes from several modules in different files.
I want to know when in the inheritance chain class C got the attribute A (then I can get the module M where C is defined and inspect the code)
consider the following code example, imagine that all classes except GrandChildOf1ChildOf2
are in other modules, is there a command, something like: attribute_base(o4,'a')
with output: Base1
?
class SweetDir:
def __sweet_dir__(self):
"""
Same as dir, but will omit special attributes
:return: string
"""
full_dir = self.__dir__()
sweet_dir = []
for attribute_name in full_dir:
if not ( attribute_name.startswith('__')
and attribute_name.endswith('__')):
#not a special attribute
sweet_dir.append(attribute_name)
return sweet_dir
class Base1(SweetDir):
def __init__(self):
super(Base1,self).__init__()
self.a = 'a'
class Base2(SweetDir):
def __init__(self):
super(Base2,self).__init__()
self.b = 'b'
class ChildOf1 (Base1):
def __init__(self):
super(ChildOf1,self).__init__()
self.c = 'c'
class GrandChildOf1ChildOf2 (Base2,ChildOf1):
def __init__(self):
super(GrandChildOf1ChildOf2,self).__init__()
self.d = 'd'
o1 = Base1()
o2 = Base2()
o3 = ChildOf1()
o4 = GrandChildOf1ChildOf2()
print(o1.__sweet_dir__())
print(o2.__sweet_dir__())
print(o3.__sweet_dir__())
print(o4.__sweet_dir__())
output:
['a']
['b']
['a', 'c']
['a', 'b', 'c', 'd']