python 将表达式传递给函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1185199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing expressions to functions?
提问by Ian P
I'm not quite sure what I mean here, so please bear with me..
我不太确定我在这里的意思,所以请耐心等待..
In SQLAlchemy, it appears I'm supposed to pass an expression to filter()
in certain cases. When I try to implement something like this myself, I end up with:
在 SQLAlchemy 中,filter()
在某些情况下,我似乎应该将表达式传递给。当我尝试自己实现这样的东西时,我最终得到:
>>> def someFunc(value):
... print(value)
>>> someFunc(5 == 5)
True
How do I get the values passed to ==
from inside the function?
如何==
从函数内部获取传递给的值?
I'm trying to achieve something like this
我正在努力实现这样的目标
>>> def magic(left, op, right):
... print(left+" "+op+" "+right)
>>> magic(5 == 5)
5 == 5
What about if one of the parameters was an object?
如果其中一个参数是一个对象呢?
回答by Nelson
You can achieve your example if you make "op" a function:
如果您使“op”成为一个函数,则可以实现您的示例:
>>> def magic(left, op, right):
... return op(left, right)
...
>>> magic(5, (lambda a, b: a == b), 5)
True
>>> magic(5, (lambda a, b: a == b), 4)
False
This is more Pythonic than passing a string. It's how functions like sort()
work.
这比传递字符串更 Pythonic。这就是sort()
工作之类的功能。
Those SQLAlchemy examples with filter()
are puzzling. I don't know the internals about SQLAlchemy, but I'm guessing in an example like query.filter(User.name == 'ed')
what's going on is that User.name
is a SQLAlchemy-specific type, with an odd implementation of the __eq()
function that generates SQL for the filter()
function instead of doing a comparison. Ie: they've made special classes that let you type Python expressions that emit SQL code. It's an unusual technique, one I'd avoid unless building something that's bridging two languages like an ORM.
那些 SQLAlchemy 示例filter()
令人费解。我不知道 SQLAlchemy 的内部结构,但我猜测在一个例子query.filter(User.name == 'ed')
中,这User.name
是一个特定于SQLAlchemy 的类型,具有为__eq()
函数生成 SQLfilter()
而不是进行比较的函数的奇怪实现。即:他们制作了特殊的类,让您可以键入发出 SQL 代码的 Python 表达式。这是一种不寻常的技术,我会避免使用这种技术,除非构建像 ORM 这样桥接两种语言的东西。
回答by Ned Deily
An even more pythonic variant of Nelson's solution is to use the operator functions from the operatormodule in the standard library; there is no need to create your own lambdas.
Nelson 解决方案的一个更加 Pythonic 的变体是使用标准库中operator模块中的operator 函数;无需创建自己的 lambda。
>>> from operator import eq
>>> def magic(left, op, right):
... return op(left, right)
...
>>> magic(5, eq, 5)
True
回答by WangST
You have to implement __eq__()
. For example ::
你必须实施__eq__()
. 例如 ::
class A(object):
def __eq__(self, other):
return (self, '==', other)
Then, for the function, which you want to get the expression, like ::
然后,对于要获取表达式的函数,例如 ::
def my_func(expr):
# deal with the expression
print(expr)
>>> a = A()
>>> my_func(a == 1)
(<__main__.A object at 0x1015eb978>, '==', 1)
回答by balpha
You can't. The expression 5 == 5
is evaluated and only then is the result passed to someFunc. The function just gets True
(the True
object, to be precise), no matter what the expression was.
你不能。对表达式5 == 5
求值,然后才将结果传递给 someFunc。无论表达式是什么,函数都只是获取True
(True
准确地说是对象)。
Edit: Concerning your edit, this questionis kind of close.
编辑:关于您的编辑,这个问题有点接近。
Edit 2: You could just pass the expression as a string and use eval, like this:
编辑 2:您可以将表达式作为字符串传递并使用 eval,如下所示:
>>> def someFunc(expression_string):
... print(expression_string, "evaluates to", eval(expression_string))
>>> someFunc("5 == 5")
5 == 5 evaluates to True
Don't know whether that helps you. Keep in mind that eval
is a powerful tool, so it's dangerous to pass arbitrary (and possibly even user-generated) input to it.
不知道对你有没有帮助。请记住,这eval
是一个强大的工具,因此将任意(甚至可能是用户生成的)输入传递给它是危险的。
回答by Ian P
It appears you can return tuples from eq:
看来您可以从eq返回元组:
class Foo:
def __init__(self, value):
self.value = value
def __eq__(self, other):
return (self.value, other.value)
f1 = Foo(5)
f2 = Foo(10)
print(f1 == f2)
回答by Brandon E Taylor
Short answer: You can't. The result of the expression evaluation is passed to the function rather than the expression itself.
简短的回答:你不能。表达式计算的结果传递给函数而不是表达式本身。