this is not a problem I need for a coding project, but a friend of mine sent this to me. The question is: what gets printed out here? and why?
x = 0
y = 0
def f():
x = 1
y = 1
class A:
print(x,y)
x = 2
f()
Output:
0 1
my first instinct told me that it should be "1 1", but I was wrong. I fiddled with the code and found out that if I remove the "x = 2" I would have been correct. but why?
also, it worked when I made the variables global. like this:
x = 0
y = 0
def f():
global x
global y
x = 1
y = 1
class A:
print(x,y)
x = 2
f()
Output:
1 1
I am not looking for other solutions to the problem, just a better understanding of what is going on here.
thanks in advance