How to retrieve source code from Python objects? - GeeksforGeeks (2024)

Last Updated : 15 Sep, 2022

Improve

We are given a object and our task is to retrieve its source code, for this we have inspect module, dill module and dis module built-in standard libraries in Python programming. They provide several useful functions to track information about live objects such as modules, classes, methods, functions, tracebacks, frame objects, and code objects. getsource() method is used to get the source code of Python objects.

Syntax: inspect.getsource(object)

Parameter: The object type parameter, of which we want the source code.

Return type: text of the source code for an object

Note: An IOError is raised if the source code cannot be retrieved.

Using the inspect module to retrieve source code

Example: Here we are importing the inspect module first and then using the inspect.getsource() function to fetch the source code of the given object.

python3

# import inspect library

import inspect

def test(x):

return x * 2

print(inspect.getsource(test))

Output:

def test(x): return (x+2)*(x-2)

Example: Here we are fetching the source code for the given object.

python3

# import inspect library

import inspect

def far(n):

factorial = 1

if int(n) >= 1:

for i in range (1, int(n)+1):

factorial = factorial * i

return factorial

source = inspect.getsource(far)

print(source)

Output:

def far(n): factorial = 1 if int(n) >= 1: for i in range (1, int(n)+1): factorial = factorial * i return factorial

Example: We can use inspect on built in library functions and objects also.

python3

# import inspect library

import inspect

import pandas

source_DF = inspect.getsource(pandas.DataFrame)

print(source_DF[:100])

Output:

class DataFrame(NDFrame): """ Two-dimensional size-mutable, potentially heterogeneous tabula

Using the dis module to retrieve source code

Here we are using the dis module in Python to fetch the source code of the specified object.

Python3

import dis

def fun(a1, a2):

# do something with args

a = a1 + a2

return a

dis.dis(fun)

Output:

 50 0 LOAD_FAST 0 (a1) 2 LOAD_FAST 1 (a2) 4 BINARY_ADD 6 STORE_FAST 2 (a) 51 8 LOAD_FAST 2 (a) 10 RETURN_VALUE

Using the dill module to retrieve source code

Here we are using the dill module in Python to fetch the source code of the specified object.

Python3

import dill

def fun(a1,a2):

a = a1 + a2

return a

source=dill.source.getsource(fun)

print(source[:200])

Output:

def fun(a1,a2): a = a1 + a2 return a


Like Article

Suggest improvement

Share your thoughts in the comments

Please Login to comment...

How to retrieve source code from Python objects? - GeeksforGeeks (2024)
Top Articles
Amazon Supply Chain Disruptions 2023 - How Sellers Should Mitigate the Risks?
Chapter 2 - Testbank - Supply Chain Management, 6e, Global Edition (Chopra/Meindl) Chapter 2 Supply - Studeersnel
2018 Jeep Wrangler Unlimited All New for sale - Portland, OR - craigslist
Radikale Landküche am Landgut Schönwalde
Skyward Sinton
25X11X10 Atv Tires Tractor Supply
Wannaseemypixels
Poe Pohx Profile
Lenscrafters Westchester Mall
Craigslist In Fredericksburg
Www Movieswood Com
Stream UFC Videos on Watch ESPN - ESPN
Baseball-Reference Com
Crusader Kings 3 Workshop
Qhc Learning
Power Outage Map Albany Ny
REVIEW - Empire of Sin
Shooting Games Multiplayer Unblocked
Letter F Logos - 178+ Best Letter F Logo Ideas. Free Letter F Logo Maker. | 99designs
Schedule 360 Albertsons
Mahpeople Com Login
Joann Ally Employee Portal
Hyvee Workday
Air Traffic Control Coolmathgames
Canvasdiscount Black Friday Deals
If you have a Keurig, then try these hot cocoa options
Asteroid City Showtimes Near Violet Crown Charlottesville
Https E22 Ultipro Com Login Aspx
Relaxed Sneak Animations
Lbrands Login Aces
Farm Equipment Innovations
Rgb Bird Flop
Miller Plonka Obituaries
What is Software Defined Networking (SDN)? - GeeksforGeeks
Sinfuldeed Leaked
N.J. Hogenkamp Sons Funeral Home | Saint Henry, Ohio
Kristen Hanby Sister Name
Sun-Tattler from Hollywood, Florida
W B Crumel Funeral Home Obituaries
The Transformation Of Vanessa Ray From Childhood To Blue Bloods - Looper
Top 25 E-Commerce Companies Using FedEx
Urban Blight Crossword Clue
20 bank M&A deals with the largest target asset volume in 2023
Firestone Batteries Prices
Kutty Movie Net
Differential Diagnosis
Nami Op.gg
National Weather Service Richmond Va
Spreading Unverified Info Crossword Clue
Okta Login Nordstrom
Craigslist Anc Ak
sin city jili
Latest Posts
Article information

Author: Prof. Nancy Dach

Last Updated:

Views: 5828

Rating: 4.7 / 5 (77 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Prof. Nancy Dach

Birthday: 1993-08-23

Address: 569 Waelchi Ports, South Blainebury, LA 11589

Phone: +9958996486049

Job: Sales Manager

Hobby: Web surfing, Scuba diving, Mountaineering, Writing, Sailing, Dance, Blacksmithing

Introduction: My name is Prof. Nancy Dach, I am a lively, joyous, courageous, lovely, tender, charming, open person who loves writing and wants to share my knowledge and understanding with you.