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

android - net_scheduler holding wakelock -

sql - MySQL : Getting Entries from a many-to-many table -

java - Retrieving data from database using jsp (Hibernate + Spring + Maven) -