如何在Python中将字符串转换为可运行的(未评估)功能
我有一个代码从脚本中创建一个代表var的字符串:
"((((x) * (320)) + ((y) * (270))) + ((z) * (400))) + ((w) * (500))"
我的目标是 store (不评估)它作为函数:
f = ((((x) * (320)) + ((y) * (270))) + ((z) * (400))) + ((w) * (500))
其中x,y,z和w是我的python脚本中的实际变量。
因此,可以通过更改一个或多个变量来动态更改它。
有图书馆这样做吗?
I have a code creating a string representing vars from my script:
"((((x) * (320)) + ((y) * (270))) + ((z) * (400))) + ((w) * (500))"
My goal is to store (not evaluate) it as a function:
f = ((((x) * (320)) + ((y) * (270))) + ((z) * (400))) + ((w) * (500))
where x, y, z and w are the actual variables in my Python script.
So that it can be dynamically changed with changing one or more of my variables.
Is there a library doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
其他答案是错误的。很可能在Python中懒惰地评估这一点。
The other answers are wrong. It's quite possible to lazily evaluate this in python.