0

I was taking a python training from udemy and suddenly found out that in python, we can assign a function to a variable and as I was practising, I found out that there are two ways it can be done. For example, suppose we are defining a function hello() as bellow:

def hello(name="Neel"):
    return "hello "+name

Now consider the two assignment: greet = hello and greet = hello()

So I want to know what are the differences between them and how's the picture behind the scene?

Saswata Mishra
  • 95
  • 1
  • 10

1 Answers1

0

greet = hello gives you a function object. When you print(greet) you will get something like <function object....>. This is because you are assigning the function hello to the variable named greet.

greet = hello() on the other hand is assigning the return value of the hello function to the variable greet. Since your default argument for name in the function hello() is Neel, thus greet = hello() essentially would result in print(greet) giving you hello Neel which is a string (and not a function object.

Hope this answers your question!

Julian Chan
  • 446
  • 2
  • 8