Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

Assume I have a Python function as characterized below: 

def foo(arg1,arg2):

    #do something with args

    a = arg1 + arg2

    return a

I can able to get the name of the function utilizing foo.func_name. How might I programmatically get its source code, as I composed previously?

1 Answer

0 votes
by (26.4k points)

In the event that the function is from a source document accessible on the filesystem, at that point inspect.getsource(foo) may be of help:

If foo is defined as:

def foo(arg1,arg2):         

    #do something with args 

    a = arg1 + arg2         

    return a  

then:

import inspect

lines = inspect.getsource(foo)

print(lines)

It returns:

def foo(arg1,arg2):         

    #do something with args 

    a = arg1 + arg2         

    return a 

In any case, I accept that if the function is gathered from a string, stream, or imported from an incorporated document, at that point you can't recover its source code.

Are you pretty much interested to learn python in detail? Come and join the python training course to gain more knowledge.

To know more about this you can have a look at the following video tutorial:-

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...