Python Nested Function Variable Assignment -


this question has answer here:

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

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -