It is very clear that, the program, the machine is working in the mapping
bar()
# in bar function you have x, but python takes it as a private x, not the global one
def bar():
x = 4
foo()
# Now when you call foo(), it will take the global x = 3
# into consideration, and not the private variable [Basic Access Modal]
def foo():
print(x)
# Hence OUTPUT
# >>> 3
Now, if you want to print 4
, not 3
, which is global one, you need to pass the private value inside the foo(), and make foo() accept an argument
def bar():
x = 4
foo(x)
def foo(args):
print(args)
# OUTPUT
# >>> 4
OR
Use global
inside your bar()
, so that machine will understand that x
inside in bar, is global x, not private
def bar():
# here machine understands that this is global variabl
# not the private one
global x = 4
foo()