python - Merging global and local scopes -


if works

x=5 def main():     globe in locals():         globals().update(locals()[globe])     print x main() 

then why doesn't this?

x=5 def main():     globe in locals():         globals().update(locals()[globe])     x+=1     print x main() 

the error in latter statement claims x referenced before assignment, works in first example...

you cannot assign global variable in python without explicitly doing so. writing x+=1 assigning value x , implicitly declaring x local variable. not defined , therefore error.

the loop has no actual effect, locals dictionary empty.

if want use global variables in python (which shouldn't, that's matter), should use global keyword.


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -