I want to be able to print out a "flattened" version of a class - viewing the source code of Class B
which inherits methods from Class A
but as if it were written as a single class without inheritance.
The Python inspect module provides the getsourcelines(B)
method, which works great for combining both A
and B
code. But, if my method in B includes an overridden class that calls super()
, this approach only shows that the super()
line exists, not what the source code in class A
contains. Here's an example:
import inspect
class A(object):
def a_method(self):
self.a = 4
return self.a
def overridden(self):
print('This is A')
class B(A):
def c_method(self):
pass
def overridden(self):
print('This is C')
print('Calling super of C:')
super().overridden()
output = ''
for name, member in inspect.getmembers(B):
#skip builtins for example
if name.startswith('__') == True:
pass
else:
lines, location = inspect.getsourcelines(member)
for line in lines:
output += line
print(output)
This prints to the command line:
def a_method(self):
self.a = 4
return self.a
def c_method(self):
pass
def overridden(self):
print('This is C')
print('Calling super of C:')
super().overridden()
Is there a way that I can replace the line super().overridden()
in the output with the contents of A.overridden()
?