简体   繁体   English

如何将一个函数作为参数传递给另一个函数,并在python中使用该函数返回的值?

[英]How to pass a function as argument to another function and use the value returned by that function in python?

I am trying to negate the value returned by one function. 我试图否定一个函数返回的值。

Consider the code to be 认为代码是

def greater(a,b):
    return a>b

check = negate(greater)

assert check(8,9)

OR 要么

def equal(a,b):
    return a==b

check = negate(equal)

assert check("python","java")

How should I define the NEGATE function??? 我应该如何定义NEGATE函数???

Use a function-style decorator : 使用函数样式的装饰器

def negate(func):
    def inner(*args, **kwargs):
        val = func(*args, **kwargs)
        return not val
    return inner

Demo: 演示:

>>> greater(10, 20)
False
>>> negate(greater)(10, 20)
True

To preserve things like docstring and other attributes of the function being passed to negate you can use functools.wraps . 为了维护之类的docstring和传递功能的其他属性来negate你可以使用functools.wraps

from functools import wraps

def negate(func):
    @wraps(func)
    def inner(*args, **kwargs):
        val = func(*args, **kwargs)
        return not val
    return inner

Like this: 像这样:

def negate(func):
  def result(*args):
    return not func(*args)
  return result

This is a function which creates a function which returns the result of invoking the original function and not ing it's return value, and then returning that function. 这个函数创建一个函数,该函数返回调用原始函数而not返回其返回值,然后返回该函数的结果。

Use the not operator in a decorator: 在装饰器中使用not运算符:

from functools import wraps

def negate(func):
    @wraps(func)
    def wrapper(*args, **kw):
        return not func(*args, **kw)
    return wrapper

The above decorator returns a wrapper function that applies not to the return value of the wrapped function. 上面的装饰器返回一个包装函数,该包装函数not用于该包装函数的返回值。 The @functools.wraps() decorator in the above example is optional but makes sure the wrapper gains the docstring and other introspectable attributes from the original function, making it behave more closely like the wrapped function. 上面的示例中的@functools.wraps()装饰器是可选的,但可确保包装器从原始函数中获取docstring和其他可自省的属性,使其行为与包装后的函数更接近。

Demo: 演示:

>>> from functools import wraps
>>> def negate(func):
...     @wraps(func)
...     def wrapper(*args, **kw):
...         return not func(*args, **kw)
...     return wrapper
... 
>>> def greater(a,b):
...     return a>b
... 
>>> check = negate(greater)
>>> check(8, 9)
True

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将返回值传递给另一个函数 - How to pass a returned value into another function 如何在python的另一个应用程序中使用函数返回的值? - How can I use a value returned by a function in another application in python? 如何在 python 中将变量作为另一个 function 的参数传递 - How to pass a variable as an argument of another function in python 如何将 function 的返回值传递给另一个 function 而这两个函数都需要来自 Python 3 中的用户的不同消息? - How to pass a returned value of a function to another function while both functions require different messages from a user in Python 3? python将函数传递给另一个函数 - python pass function with argument to another function 将函数参数作为参数传递给python中的另一个函数 - pass a function parameter as an argument to another function in python 如何将python函数返回的参数传递给程序并使用GDB进行分析? - How to pass an argument returned by a python function to a program and analyse it with GDB? Python:如何将函数作为参数传递给另一个函数,而不自动运行它 - Python: How to pass function as an argument to another function, without running it automatically 如何在另一个 function 中使用前一个 function 返回的变量? (Python) - How to use a returned variable from a previous function in another function? (python) python lambda“绑定变量”如何与返回的 function 值一起工作,该值又作为另一个参数传递? - How does a python lambda 'bound variable' work with a returned function value which is in turn passed as another argument?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM