Ananthu Ananthu - 1 month ago 10
Python Question

Whats the difference between function calling with out paranthesis in python?

Here I got 2 sets of code

def outer(msg):
def inner(msg):
return inner()
outer("hi")


The other one is

def outer(msg):
def inner(msg):
return inner
outer("hi")


Whats the difference between these two set?

Answer

Parentheses are the syntax for calling a function. Not strictly an operator but you could consider it that way:

inner()    # Call the function

Functions have return values (default is None), so

return inner()

Calls the inner function and returns whatever inner returns.

The name inner is just a reference to an object.

x = 42

The name x is a reference to an int.

def inner():
    pass

The name inner is a reference to a function. x = inner followed by x() is perfectly valid.

So returning a name, any name, is just returning a reference to the object to which it refers, whatever that might be. A function, an integer, an elephant, whatever.