如何在Python中将字符串转换为可运行的(未评估)功能

发布于 2025-01-26 06:28:09 字数 350 浏览 1 评论 0原文

我有一个代码从脚本中创建一个代表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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

邮友 2025-02-02 06:28:09

其他答案是错误的。很可能在Python中懒惰地评估这一点。

>>> create_function=lambda s:lambda:eval(s)
>>> x=2
>>> y=3
>>> create_function('x+y')()
5
>>> z=create_function('x*y')
>>> z()
6

The other answers are wrong. It's quite possible to lazily evaluate this in python.

>>> create_function=lambda s:lambda:eval(s)
>>> x=2
>>> y=3
>>> create_function('x+y')()
5
>>> z=create_function('x*y')
>>> z()
6
不奢求什么 2025-02-02 06:28:09
import re
from typing import List, Any
from dataclasses import dataclass, field

@dataclass
class Database:
    variables: List[str] = field(default_factory=lambda: [])
    expression: str = ''

def store_expression(database: Database, plain_expression: str) -> None:
    variable_regexes = r'[a-z]'
    # you can get the variables as str and store them
    database.variables = re.findall(variable_regexes, plain_expression)
    
    # Then you can replace the equation with {} and store it too
    database.expression = re.sub(r'[a-z]', '{}', plain_expression)

def eval_expression(database: Database) -> Any:
    return eval(
        database.expression.format(
            *database.variables
        )
    )

s = "((((x) * (320)) + ((y) * (270))) + ((z) * (400))) + ((w) * (500))"

in_memory_db = Database()
store_expression(in_memory_db, s)

x = 1
y = 2
z = 3
w = 4

result = eval_expression(in_memory_db)
print(result)
import re
from typing import List, Any
from dataclasses import dataclass, field

@dataclass
class Database:
    variables: List[str] = field(default_factory=lambda: [])
    expression: str = ''

def store_expression(database: Database, plain_expression: str) -> None:
    variable_regexes = r'[a-z]'
    # you can get the variables as str and store them
    database.variables = re.findall(variable_regexes, plain_expression)
    
    # Then you can replace the equation with {} and store it too
    database.expression = re.sub(r'[a-z]', '{}', plain_expression)

def eval_expression(database: Database) -> Any:
    return eval(
        database.expression.format(
            *database.variables
        )
    )

s = "((((x) * (320)) + ((y) * (270))) + ((z) * (400))) + ((w) * (500))"

in_memory_db = Database()
store_expression(in_memory_db, s)

x = 1
y = 2
z = 3
w = 4

result = eval_expression(in_memory_db)
print(result)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文