简体   繁体   English

Z23EEEB4347BDD255DDFC6B7EE9AB 中的返回 function 和返回 function object 有什么区别?

[英]What is the difference between return function and return function object in python?

def speak():
    def whisper():
        print('ased')
    return whisper()

speak()

While using decorators what we do is return function object, but here we have returned the function (return whisper()), so can someone explain what is the difference between returning a function and returning a function object? While using decorators what we do is return function object, but here we have returned the function (return whisper()), so can someone explain what is the difference between returning a function and returning a function object?

The return function object, returns the function and in Python can be assigned to another function which also becomes callable function. The return function object, returns the function and in Python can be assigned to another function which also becomes callable function. The new assigned function can be called to perform the same task.可以调用新分配的 function 来执行相同的任务。

Where as in return function, it returns the value and only the same function will be called to perform the function.而作为返回 function,它返回值并且只有相同的 function 将被调用来执行 function。 It can not be assigned.它不能被分配。

when you return the function, it will execute the print('ased') when create a new variable.当您返回 function 时,它将在创建新变量时执行 print('ased')。 But if you return the object, you can execute the variable that will perform the same task但是如果您返回 object,您可以执行将执行相同任务的变量

You are writing a function that has a nested function inside it.您正在编写一个 function ,其中有一个嵌套的 function 。 The normal intent of this is that you would have some other code in there more interesting than what you are doing, but I realize this is just a toy for you to try the idea, so we can ignore that.这样做的正常意图是你会有一些其他代码比你正在做的更有趣,但我意识到这只是你尝试这个想法的玩具,所以我们可以忽略它。

The real problem you have is the return.您遇到的真正问题是退货。 You code:你编码:

def speak():
    def whisper():
        print('ased')
    return whisper()

speak()

has the return statement actually calling the whisper() function and then returning the result of that function call, which is nothing since whisper() does not return anythin it just prints. return 语句实际上调用了whisper() function,然后返回了 function 调用的结果,这没什么,因为whisper()没有返回任何它只是打印的东西。

So it is basically the same as if you had written:所以它基本上和你写的一样:

def speak():
    return print('ased')

speak()

or just:要不就:

def speak():
    print('ased')

speak()

What you probably intended was to leave off the () 's like this return whisper so that it did not call the function, but actually returned a reference to the function that would be returned and could later be called, like this:您可能打算放弃()这样的return whisper ,这样它就不会调用 function,而是实际上返回了对 function 的引用,该引用将被返回并且以后可以调用,如下所示:

def speak():
    def whisper():
        print('ased')
    return whisper

func = speak()
...
func()

Which would not print anything when speak was called but would return a function reference that you can save and then call later (in this case func ).调用 speak 时不会打印任何内容,但会返回 function 引用,您可以保存并稍后调用(在本例中为func )。

The above is not the way a decorator works, but it does use the same principal of defining an internal function and then returning a reference to that function.以上不是装饰器的工作方式,但它确实使用了相同的原理,即定义内部 function,然后返回对该 function 的引用。

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

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