Default arugment of the get method for dictionaries in Python 3


#1

Hi, I have this simple function that uses a dictionary to choose which function to be executed. In the default argument of the get method, there is a string (“Invalid month”). This does not correspond to any function. So it is a string. But what if func = “Invalid month”. Then why does print(func()) print invalid month. print(“Invalid Month”()) doesn’t seem right. It’s a string and has the syntax of a function? Can someone please explain this to me. Thanks.

Also, if you had

def f():
    print(4)

variable = f

Why does f() and variable() do the same thing. How can a variable that holds a function have the syntax of a function.


#2

The variable does not hold the function. It is simply a name that points to the function object in memory. If we set another variable to point to the same object, it will just be another reference, not another function.

variable() and f()

are references to the same function.

The function is the lambda, which is what executes and returns a string, which gets printed.