Suppose we have following code defined in tester.py
class Tester( object ):
def method( self ):
print 'I am a Tester'
and we have following defined in main.py
from tester import Tester
t = Tester()
#print definition of t
is there anyway we could get the definitions of a class/function from an object in a systematic way? or we have to parse the code and extract the code definition manually then save them into a string?
You can use the inspect
module:
import inspect
class Tester( object ):
def method( self ):
print 'I am a Tester'
print inspect.getsource(Tester)
Output:
class Tester( object ):
def method( self ):
print 'I am a Tester'
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