035-001. global variable, local variable, global namespace, locals()
@
# Let's talk about "use range of variable"
# This is global variable
x = 10
def foo():
print(x)
# global scope is range of area where global variable can affect
@
def foo():
# This is local variable
x = 10
print(x)
foo()
print(x)
# output:
# error
# local scope
@
Let's try change value of global variable inside of method
x = 10
def foo():
# x is actually local variable named x
x = 20
print(x)
foo()
# output:
# 20
print(x)
# output:
# 10
@
# If you want to change value of global variable inside of method,
# you need to use "global" keyword inside of method
x = 10
def foo():
global x
x = 20
print(x)
foo()
# output:
# 20
print(x)
# output:
# 20
@
# When there is no global variable outside of method,
# if you use global keyword inside of method,
# affected local variable by global keyword inside of method becomes global variable
def foo():
# Since there is no global variable named x,
# this local variable x becomes global variable x
global x
x = 20
print(x)
@
# Namespace in Python
# In Python, variable is stored in namespace
# If you use locals(), you can print contents of current namespace as dictionary type
x = 10
locals()
# output:
# {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': , '__spec__': None, '__annotations__': {}, '__builtins__': , 'x': 10}
# If you see printed contents of namespace, you can see 'x':10 is stored in namespace
# If you invoke locals() in global scope, you will print global namespace
@
# You can invoke locals() in method
def foo():
x = 10
print(locals())
foo()
# output:
# {'x': 10}
# In local namespace, only 'x': 10 is stored in it