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

前端 未结 5 1828
心在旅途
心在旅途 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:45

    A function is an object just like any other object in Python. If you define a new function, a new function object is created and is assigned to the name of the function.

    def spam():
        print('The knights who say Ni!')
    
    print(spam) # => <function spam at 0x7fc64efc6f28>
    
    spam() # => output: 'The knights who say Ni!
    

    As you can see after the function was defined it got assigned to the name of spam. It is essentially similar to an assignment statement being run. Now that name is going to give you access to the function but in order to actually use it you have to call it using the parentheses like spam(). If you don't call it using the parentheses, it will just return the function object that has been assigned to the name spam.

    0 讨论(0)
  • 2020-12-01 20:46

    hello refers to the function object, hello() calls the function itself.

    For example:

    >>> def hello():
    ...     print "hello"
    ... 
    >>> hello
    <function hello at 0x106c91398>
    >>> hello()
    hello
    
    0 讨论(0)
  • 2020-12-01 20:53

    In short, function with parenthesis () is actually calling (invoke) that function whereas function without parenthesis () is an object of that function which can be passed as an argument and later be called by appending () to it.

    def hello():
        return 1
    

    i.e.
    hello >> returns object
    hello() >> outputs 1

    0 讨论(0)
  • 2020-12-01 20:58

    TL;DR: the parentheses execute the function itself; without them you treat the function as it were a variable

    In python you append the parentheses (with optional variables inside) to the function name only when you want to invoke it.

    But there is also another way to use function names. In fact, in python functions are first class objects, that means that the function can be passed around as if it where a variable name. To do you must not append the parentheses () at the end of the function name.

    Check the following:

    def f():
        return 'f'
    
    print( type(f) ) # result: <class 'function'>
    print( type(f()) ) # result: <class 'str'>
    

    As you can see, in the first instance we print the type of f (without parentheses), which is a function

    In the second instance we print the type of f() (with the parentheses), which is a string, that is the type returned by f when it is invoked.

    This behavior is useful to pass functions as parameters of other functions, etc, etc.

    The fact that id() and id(function-invoked>) returns different ids:

    print(id(hello))   # here it returns the id of the function name
    
    print(id(hello())) # here it returns the id 
                       # of the object returned by the function itself
    
    0 讨论(0)
  • 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))
    
    0 讨论(0)
提交回复
热议问题