If the function is from a source file available on the filesystem, then inspect.getsource(foo) might 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)
Returns:
def foo(arg1,arg2):
#do something with args
a = arg1 + arg2
return a
But I believe that if the function is compiled from a string, stream or imported from a compiled file, then you cannot retrieve its source code.
Answer from Rafał Dowgird on Stack OverflowHow can I get the source code of a Python function? - Stack Overflow
Using the "inspect" module to print the source code of a function
Python's introspection methods
How can I display the code for a Python method along with the code for its nested functions, specifying a particular depth?
Will you be ok to add a custom decorator to your functions? If yes I think it is pretty easy, in a file You can do this:
from inspect import getsource
import my_module
def code_printer(func):
print(getsource(func))
return func
if name == '__main__': ...In my_module there is your functions. And your function, in my_module will look like:
from printer import code_printer
@code_printer
def custom_function():
do something herePrinter is the name of the file that I created where the decorator code_printer is defined. The decorator does not change the function, since it just returns it, but allows you to print the function code even without executing the function.
What you think?
More on reddit.comVideos
If the function is from a source file available on the filesystem, then inspect.getsource(foo) might 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)
Returns:
def foo(arg1,arg2):
#do something with args
a = arg1 + arg2
return a
But I believe that if the function is compiled from a string, stream or imported from a compiled file, then you cannot retrieve its source code.
The inspect module has methods for retrieving source code from python objects. Seemingly it only works if the source is located in a file though. If you had that I guess you wouldn't need to get the source from the object.
The following tests inspect.getsource(foo) using Python 3.6:
import inspect
def foo(arg1,arg2):
#do something with args
a = arg1 + arg2
return a
source_foo = inspect.getsource(foo) # foo is normal function
print(source_foo)
source_max = inspect.getsource(max) # max is a built-in function
print(source_max)
This first prints:
def foo(arg1,arg2):
#do something with args
a = arg1 + arg2
return a
Then fails on inspect.getsource(max) with the following error:
TypeError: <built-in function max> is not a module, class, method, function, traceback, frame, or code object
Suppose you have a function, and you want to print its source code.
You can do it like this:
import inspect
# Define a function
def my_function():
x = 1
y = 2
z = x + y
return z
# Print the source code of the function using inspect.getsource
source_code = inspect.getsource(my_function)
print(source_code)The "inspect.getsource" function is used to get the source code of the "my_function" function. The "getsource" function takes a function object as its argument and returns a string that contains the source code of the function.
This trick is useful when you want to inspect the source code of a function, especially if the function is defined in a third-party library or module.