print the code which defined a lambda function [duplicate]

As long as you save your code to a source file you can retrieve the source code of
an object using the inspect module.

example:
open editor type:

myfunction = lambda x: x==2

save as lamtest.py

open shell type python to get to interactive python
type the following:

>>>from lamtest import myfunc
>>>import inspect
>>>inspect.getsource(myfunc)

the result:

'myfunc = lambda x: x==2\n'

Leave a Comment