-3
def useless(x):
    print(x)
useless(5)
print(x)

Why doesn't the last line of my code print "5" (referring to the function parameter used in line three bound to x)?

mattliang
  • 49
  • 1
  • 7

1 Answers1

0

There is no variable x in that scope. When you called useless(5), you just assigned x = 5 in the scope of the useless function. But when you quit the function, your scope changed and there is simply no x defined outside of the function. You can check it with the locals() call, it will show you all the assigned variables (even default ones) in your current scope.

Joe Samanek
  • 1,644
  • 12
  • 16