Modify the function variables from inner function in python -
Modify the function variables from inner function in python -
it's ok , print outer function variable a
def outer(): = 1 def inner(): print it's ok outer function array a , append something
def outer(): = [] def inner(): a.append(1) print however, caused problem when tried increment integer:
def outer(): = 1 def inner(): += 1 #or = + 1 print >> unboundlocalerror: local variable 'a' referenced before assignment why happen , how can accomplish goal (increase integer)?
workaround python 2:
def outer(): = [1] def inner(): a[0] += 1 print a[0] python function variables scope
Comments
Post a Comment