简体   繁体   English

Python 3.x-返回另一个函数的简单函数

[英]Python 3.x - Simple function that returns another function

I just recently started programming in Python and I've been trying to create a simple function that takes two parameters, a and b, and returns the result of the sum of a and |b|. 我最近刚开始使用Python进行编程,我一直在尝试创建一个简单的函数,该函数带有两个参数a和b,并返回a和| b |的和的结果。 I want to return f(a, b) and not just f. 我想返回f(a,b)而不仅仅是f。 I know that I'm assigning f to be an int in my current code and so it returns "int not callable error" when I run. 我知道我在当前代码中将f分配为int,因此在运行时它会返回“ int not callable error”。 So I have to assign f to be a function of some sort. 因此,我必须将f分配为某种函数。 I'm fairly certain I'm missing something fundamental here, I'm just not sure exactly what. 我敢肯定,我在这里遗漏了一些基本知识,只是不确定到底是什么。 Thanks for any help! 谢谢你的帮助!

from operator import add, sub

def a_plus_abs_b(a, b):
    """Return a+abs(b), but without calling abs.

    >>> a_plus_abs_b(2, 3)
    5
    >>> a_plus_abs_b(2, -3)
    5
    """
    if b < 0:
        f = sub(a, b)
    else:
        f = add(a, b)
    return f(a, b)
f = sub(a, b)

doesn't create a function it computes the result, so when you're calling f(a, b) you're calling an integer. 不会创建函数,它不会计算结果,因此,当您调用f(a, b)您正在调用整数。

To fix it, assign the function in-line with a ternary to select which function to create depending on the sign of b 要修复此问题,请为函数内联三元数,以根据b的符号选择要创建的函数

f = sub if b < 0 else add

Jean-Fançois's answer is great, but if you're not understanding the fundamentals behind this, you should look into another example that uses lambdas: Jean-Fançois的答案很好,但是如果您不了解其背后的基本原理,则应查看另一个使用lambda的示例:

def returns_function_example():
    return lambda arg1: arg1 + 2

function_returned = returns_function_example()

print(function_returned(1))

# Output = 3

Run this in your debugger to see that "function_returned" in indeed a function. 在调试器中运行此命令,以查看确实存在功能的“ function_returned”。 You can connect the dots after the "print" function is called. 您可以在调用“打印”功能后连接点。

Functions are first-class citizens in Pythonland, so that you can manipulate them as any other object. 函数是Pythonland中的一等公民,因此您可以像其他任何对象一样操作它们。

Suppose you create your function: 假设您创建了函数:

def add(a, b): return a + b

If you write add , that's a function. 如果编写add ,那就是一个函数。 But if you write add(2,4) , or add(a, b) assuming that a and b are defined, then you are calling the function, so you get a result. 但是,如果您假设定义了a和b来编写add(2,4)add(a, b) ,则您正在调用该函数,因此会得到结果。 These are two completely different things: there is f , a callable , and f(a,b) which returns a value. 这是两个完全不同的东西:有f ,一个callablef(a,b)返回一个值。

So if you write: 因此,如果您写:

>>> f = add
>>> type(f)
<class 'function'>

That's what you want, you now have a function that does exactly what add does (and you could say it actually is add). 那就是您想要的,现在您有了一个功能完全add执行add功能(您可以说它实际上 add)。

By contrast, if you write: 相反,如果您写:

>>> f = add(a,b)
>>> type(f)
<class 'int'>
>>> f
11

That's a value. 那是一个价值。

So to make a long story short: 因此,总而言之:

from operator import add, sub

def a_plus_abs_b(a, b):
    """
    Return a+abs(b), but without calling abs.

    >>> a_plus_abs_b(2, 3)
    5
    >>> a_plus_abs_b(2, -3)
    5
    """
    if b < 0:
        f = sub
    else:
        f = add

    return f(a, b)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM