Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Viewing the code of a Python function [duplicate]

Let's say I'm working in the Python shell and I'm given a function f. How can I access the string containing its source code? (From the shell, not by manually opening the code file.)

I want this to work even for lambda functions defined inside other functions.

like image 890
Ram Rachum Avatar asked Oct 25 '10 12:10

Ram Rachum


People also ask

How do I see the code of a function in python?

We use the getsource() method of inspect module to get the source code of the function. 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.

How do you clone a function in python?

Python – clone() function in wand libraryclone() function makes an exact copy of the original image. One can use this clone image to manipulate without affecting the original image. clone() is one of the most important function because it helps in safe manipulation of image.

Where is python source code?

The source for python 2.7 itself can be found at http://hg.python.org/cpython. Other versions of python have had their source imported onto Launchpad. You can see them here. Click on one you want to see and you can then click "Browse the Code".


1 Answers

inspect.getsource
It looks getsource can't get lambda's source code.

like image 179
sunqiang Avatar answered Sep 28 '22 11:09

sunqiang