简体   繁体   English

函数可以返回另一个 function 用于 Python

[英]Functions can return another function use in Python

It's not hard to understand "functions are objects we can return a function from another function".不难理解“函数是我们可以从另一个函数返回 function 的对象”。 But how is below code working?但是下面的代码是如何工作的?

# Functions can return another function 

def create_adder(x): 
    def adder(y): 
        return x+y 

    return adder 

add_15 = create_adder(15) 

print (add_15(10)) 

The result is 25.结果是 25。

Personal understanding:个人理解:

create_adder(x) function will return the reference of adder function, kind like: create_adder(x) function 将返回adder function 的引用,类似于:

<function create_adder.<locals>.adder at 0x7fd83e19fe18>

15 is x in it(create_adder) and add_15 is object of create_adder function, so add_15(10) might have taken x as the argument.Then how did it get the value of y?No variable created for it and no argument passed for it? 15是其中的x(create_adder),add_15是create_adder function的object,所以add_15(10)可能将x作为参数。那么它是如何得到y的值的?没有为它创建变量并且没有为它传递参数?

Can someone help me point out the misunderstanding?有人可以帮我指出误解吗?

A couple more comments should make it clear:还有一些评论应该清楚:

# Functions can return another function 

def create_adder(x): 
    def adder(y): 
        return x+y 

    return adder 


add_15 = create_adder(15) 

# def create_adder(15): 
#     def adder(y): 
#         return 15+y 

#     return adder 


print (add_15(10)) 

# add_15(10) = adder(10)
# adder(10) # returns 15 + 10 = 25

add_15 = create_adder(15) add_15 = create_adder(15)
-----> here, x will be assigned as 15 and create_adder(15) will have the address of adder function -----> 这里,x 将被分配为 15,create_adder(15) 将具有加法器的地址 function

print (add_15(10)) -----> that address will be passed here to assign 10 to y, where already x is having value of 15, and x+y ie 25 will be returned print (add_15(10)) -----> 该地址将被传递到这里以将 10 分配给 y,其中 x 的值已经是 15,并且 x+y 即 25 将被返回

暂无
暂无

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

相关问题 如何在 python 中使用另一个 function 中的变量? 函数的唯一区别是 return 语句 - How to use variables from one function in another in python? Where the only difference in functions is the return statement Python - 使用函数中另一个模块的函数 - Python - use functions from another module in a function 如何在 Python 3 中获取一个函数以返回我可以在另一个函数中使用的值? - How can I get a function in Python 3 to return a value that I can use in another function? 在Python的另一个函数中使用函数时,是否可以隐藏函数的某些返回值? - Can I hide some of my function's return values when I use it inside another function in Python? 无法使创建的 python function 返回一个值,然后我可以在另一个计算中使用该值 - Can't make created python function return a value which I can then use in another calculation 我可以使用来自另一个函数的类型信息作为 Python 中的返回类型吗? - Can I use the typing information from another function as the return type in Python? Python返回另一个函数 - Python return for another function 函数如何调用同一类python中的另一个函数 - How can function call another functions in same class python 我在 python 中有一个方法/函数,我试图在其中获取一个返回值,然后我可以将其用作另一个函数中的变量 - I have a method / function in python in which i'm trying to get a return value which i can then use as a variable in another function 无法使用另一个函数/ python文件中的返回值 - Cant use a return value from another function/python file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM