Why the first code outputs 51 and the second code outputs 21. I understand the second code should output 21, but the way I understood, the first code should also output 21 (The value of b changed to 20 and then is calling the function f). What am I missing?
b = 50
def f(a, b=b):
return a + b
b = 20
print(f(1))
Output: 51
b = 50
b = 20
def f(a, b=b):
return a + b
print(f(1))
Output: 21
Edit: This is different from How to change default value of optional function parameter in Python 2.7? because here the unintentional change happening to the default parameter is being discussed, not how to intentionally change the value of default parameter, ie here the question focuses on how the python interpreter treats the position of function definition for functions having default parameters.