4
votes

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 an object with value equals this function. Or maybe am I wrong? What happens when I call it without parentheses?

I wonder why

def hello():
    pass

print(id(hello))
print(id(hello()))

returns different results

4400606744
4398942536
5
you can't "call" a function without parenthesis. hello() is a function call, hello is not. hello is just the function, not a call to it. - Zinki
@ASMackay I have updated my question. It doesn't look like a duplicated question now, isn't it? - user7304253
It's different because in the first case you're getting the id of the function object itself, and in the second you get the id of whatever the function returns (here: None). - deceze♦
@deceze, you are right. When I write print(id(None)) I get the same number as print(id(hello())). - Hrvoje T

5 Answers

4
votes

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))
3
votes

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
2
votes

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

1
votes

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
votes

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.