python - local variable - run time error with local -
x=5 def printx() print x x=10
running gives unboundlocal error
but when function print x no error..
simply assigning value x
in function making local variable, therefore shadowing global x
specified on previous line. , on line you're trying print it, local version of x
hasn't been initialized yet. curious how doing on later line influencing lines come before it, that's how works.
you don't need special declaration read global variable, therefore works without assignment. however, if you'd rather assign global x
instead of making new, local x
, you'll have specify global x
before assigning it.
Comments
Post a Comment