Suppose I have a Python function as defined below:
def foo(arg1,arg2):#do something with argsa = arg1 + arg2return a
#do something with args
a = arg1 + arg2
I can get the name of the function using foo.func_name. How can I programmatically get its source code, as I typed above?
To get the of your function you programmatically you can use the inspect module which has methods for retrieving the source code from python objects. One thing to note it would only work if the source is located in the same file where you are writing the below-mentioned code. Below is the code that shows how to use the inspect module:-
import inspectlines = inspect.getsource(foo)print(lines)
lines = inspect.getsource(foo)
Certification in Digital Marketing |
Certification in Big Data Analytics |
Certification in Cloud & Devops |
Certification in Full Stack Web Development
Big Data and Data Science Master’s Course |
Data Science Architect Master’s Program |
Cloud and DevOps Architect Master’s Course |
Artificial Intelligence Engineer Master’s Course |
Microsoft Azure Certification Master’s Training
Data Science Course Online |
Big Data Hadoop Certification Training |
Machine Learning Course Online |
Artificial Intelligence Course and Training |
Salesforce Certification Training: Administrator and App Builder |
Tableau Training and Certification Course |
Python Data Science Course & Training |
Ethical Hacking Course |
Business Analyst Course |
Courses in Cyber Security
© COPYRIGHT 2011-2021 INTELLIPAAT.COM. ALL RIGHTS RESERVED.
Download Salary Trends Now !
Learn how professionals like you got up to 100% Salary Hike.