Running this file foo.py
import inspect
class Parent(object):
def source1(self):
class A(object):
def foo(self):
pass
print inspect.getsource(A)
def source2(self):
class A(object):
def bar(self, a, b, c):
pass
print inspect.getsource(A)
parent = Parent()
parent.source1()
parent.source2()
produces this output:
class A(object):
def foo(self):
pass
class A(object):
def foo(self):
pass
when I expect it to produce this output:
class A(object):
def foo(self):
pass
class A(object):
def bar(self, a, b, c):
pass
I am encountering this problem in my tests. I reuse the names A
and B
for class names within the body of the test method. I'm testing a function that relies on inspect.getsource
and, well... it doesn't work because later tests are given the source from earlier-named A
and B
classes.