Example1:
x =[]
def func():
print('x value is:', x)
x.append('member1')
print('x value is now:', x)
func()
print('x value is', x)
After run, the result will
('x value is:', [])
('x value is now:', ['member1'])
('x value is', ['member1'])
example2:
y = 50
def func():
#global y
print('y value is:', y)
y = 2
print('global y is change to:', y)
func()
print('y value is', y)
after run:
UnboundLocalError: local variable 'y' referenced before assignment
the solution is un-commment the line global x
but why python treat list and interge in different way ?
why list x don't require global?
why python don't respond for example 1 for list like this:
local variable 'x' referenced before assignment
Thanks!