Python Nested Function Variable Assignment -
this question has answer here:
- python variable scope error 10 answers
i'm trying along following lines in python3:
i = 1337 def g(): print(i) = 42 g()
but following error
unboundlocalerror: local variable 'i' referenced before assignment
i think understand error message means, why case? there way circumvent this?
in 2 words - when given variable name not assigned value within function references variable looked up. use global - , in such case python in global scope:
i = 1337 def g(): global print = 42 g()
you can read more on variable scopes in pep-0227
Comments
Post a Comment