

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to retrieve source code from Python objects?
We use the getsource() method of inspect module to get the source code of the function.
inspect.getsource(object)
Returns the text of the source code for an object. The argument may be a module, class, method, function, traceback, frame, or code object. The source code is returned as a single string. An IOError is raised if the source code cannot be retrieved.
If the function is compiled from a string, stream or imported from a compiled file, then you cannot retrieve its source code.
We import the inspect module and retrieve the source code for given script as follows
Example
#baz.py import inspect class foo: def bar(): print 'Hello' print(inspect.getsource(foo))
Output
C:/Users/TutorialsPoint1/~.py class foo: def bar(): print 'Hello'
- Related Questions & Answers
- Python Code Objects
- Accessing HTML source code using Python Selenium.
- Retrieve property value selectively from array of objects in JavaScript
- Source code of ABAP reports without restriction
- Rearrange the given source code in C++
- Decrypting source message from a code based on some algorithm in JavaScript
- How to retrieve the MSI package product code using PowerShell?
- How can I source a Python file from another Python file?
- How to Find out the source code of a transaction in SAP?
- How to represent the source code of an object with JavaScript Arrays?
- Installing MySQL from Source
- Displaying Source code of RFC in SAP system
- How to retrieve Python module path?
- How to access Python objects within objects in Python?
- How to Fetch source code of the transaction note in SAP R/3?
Advertisements