打印定义lambda函数的代码[重复](print the code which defined a lambda function [duplicate])
这个问题在这里已经有了答案:
我如何获得Python函数的源代码? 10个答案我希望能够打印一个lambda函数的定义代码。
例如,如果我通过lambda语法定义了这个函数:
>>>myfunction = lambda x: x==2 >>>print_code(myfunction)我想要得到这个输出:
x==2This question already has an answer here:
How can I get the source code of a Python function? 13 answersI'd like to be able to print the definition code of a lambda function.
Example if I define this function through the lambda syntax:
>>>myfunction = lambda x: x==2 >>>print_code(myfunction)I'd like to get this output:
x==2最满意答案
只要您将代码保存到源文件,您就可以使用inspect模块检索对象的源代码。
例如:打开编辑器类型:
myfunction = lambda x: x==2保存为lamtest.py
打开shell类型的python来获得交互式python类型如下:
>>>from lamtest import myfunc >>>import inspect >>>inspect.getsource(myfunc)结果:
'myfunc = lambda x: x==2\n'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==2save 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'更多推荐
发布评论