What is the difference between calling function with parentheses and without in python?

前端 未结 5 1849
心在旅途
心在旅途 2020-12-01 20:15

I have a question. Lets assume that we have function hello(). What is the difference between calling it with parentheses and without? When I call hello() it is referring to

5条回答
  •  误落风尘
    2020-12-01 20:59

    Short answer: see https://nedbatchelder.com/text/names.html to get a better understanding of the difference between objects and the names used to refer to objects.


    A function is called if and only if you use parentheses. hello() calls the function; hello is simply a name bound to the function, and can be used, for example, to pass the function object as an argument to another function.

    def caller(f):
        f()
    
    def hello():
        print("hi")
    
    def goodbye():
        print("bye")
    
    caller(hello)  # Prints "hi"
    caller(goodbye)  # Prints "bye"
    

    Regarding your update, id returns different values because each call to id receives a completely separate object as its argument. With id(hello), id gets the function object itself. With id(hello()), id is getting the object returned by the call to hello; it's the same as

    x = hello()
    print(id(x))
    

提交回复
热议问题