您如何以字符串形式读取lambda函数? [英] How do you read a lambda function as a string?
本文介绍了您如何以字符串形式读取lambda函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
创建完一个lambda函数后,我想阅读它.
I'd like to read a lambda function I created as a string, after I created it.
例如,
func = lambda num1,num2: num1 + num2
我想将func读为:
'lambda num1,num2: num1 + num2'
有没有一种方法可以做到这一点,甚至可以读取lambda函数的任何部分?
Is there a way to accomplish this or even read any part of the lambda function?
推荐答案
编辑:由于我误解了问题,因此更改了我的第一个答案.这个答案是从许多其他用途中借用的,但是我已经完成了代码,仅显示所需字符串的一部分.
Edit: Changed my first answer as I misunderstood the question. This answer is borrowed from a number of other uses, however I have completed the code to only display the part of the string that you want.
import inspect
func = lambda num1,num2: num1 + num2
funcString = str(inspect.getsourcelines(func)[0])
funcString = funcString.strip("['\\n']").split(" = ")[1]
print funcString
输出以下字符串:
lambda num1,num2: num1 + num2
这篇关于您如何以字符串形式读取lambda函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文