0

I encountered a situation that confused me about global and local scopes.

x = 25
def display(a, b=x):
    print(a, b)
            
x = 15

Output:

>>> display(3)
3 25

How is the output is 3 25 and not 3 15?

Ch3steR
  • 20,090
  • 4
  • 28
  • 58
Jitendra
  • 461
  • 6
  • 15
  • 2
    Function default arguments are calculated when the function is _defined_, not when it is _called_. At the time the function was defined, x was 25. – John Gordon Dec 13 '21 at 15:38
  • [This answer](https://stackoverflow.com/a/1133013/476) in the duplicate explains it best. – deceze Dec 13 '21 at 15:39

0 Answers0