Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:pass";不是";作为lambda函数_Python_Lambda - Fatal编程技术网

Python:pass";不是";作为lambda函数

Python:pass";不是";作为lambda函数,python,lambda,Python,Lambda,我需要传递一个函数作为一个参数,作为布尔值“not”使用。我尝试了类似的方法,但没有成功,因为not不是函数 theFunction(callback=not) # Doesn't work :( 我需要执行以下操作,但是我想知道是否存在任何预定义的函数来完成这个简单的工作,这样我就不必像这样重新定义它了: theFunction(callback=lambda b: not b, anotherCallback=lambda b: not b) 注意:我无法改变这样一个事实,即我必须传递

我需要传递一个函数作为一个参数,作为布尔值“not”使用。我尝试了类似的方法,但没有成功,因为
not
不是函数

theFunction(callback=not) # Doesn't work :(
我需要执行以下操作,但是我想知道是否存在任何预定义的函数来完成这个简单的工作,这样我就不必像这样重新定义它了:

theFunction(callback=lambda b: not b, anotherCallback=lambda b: not b)


注意:我无法改变这样一个事实,即我必须传递这样一个函数,因为它是一个API调用。

是的,有
操作符
模块:


not
不是一个函数,而是一个关键字。这意味着你不能传递一个引用。有很好的理由,因为它允许Python“短路”某些表达式

但是,您可以使用
运算符
程序包的
not_uu
(带下划线):

from operator import not_

theFunction(callback=not_, anotherCallback=not_)
不从操作员导入_

函数(callback=not_uuu,另一个callback=not_u)
而不是你的lambda示例,你可能仍然习惯于使用
def not_ub(b):返回not b
,然后
函数(callback=not_uuu,另一个callback=not_u)
。这是什么?@PatrickCollins一如既往:看到顶部的横幅。
from operator import not_

theFunction(callback=not_, anotherCallback=not_)