What is the difference between these two? (Assigning functions to variables)

Max Nathaniel Ho maxhowl86 at gmail.com
Tue Nov 4 22:17:35 EST 2014


Just to be clear, I was referring to these two lines

greet = compose_greet_func() 

greet_someone = greet 

On Wednesday, November 5, 2014 11:15:46 AM UTC+8, Max Nathaniel Ho wrote:
> Example 1
> 
> def compose_greet_func():
>     def get_message():
>         return "Hello there!"
> 
>     return get_message
> 
> greet = compose_greet_func()
> print greet()
> 
> 
> Example 2 
> 
> def greet(name):
>     return "hello "+name
> 
> greet_someone = greet
> print greet_someone("John"
> 
> In Example 1, the function compoe_greet_func is assigned to the variable greet, and () is included at the end of the function.
> 
> However, in Example 2, the function greet is assigned to the variable greet_someone but () is excluded at the end of the function.
> 
> Does the () matter when assigning functions to variables? 
> 
> Thank you!



More information about the Python-list mailing list