on-internet
← Community

Is there any use for a top-level global variable declaration?

Stack Overflow ·

In Python, the global declaration is used inside function definitions to specify that assigning the variable will create/update a global variable, rather than the default local variable. Python also allows the global statement outside of function definitions. This doesn't cause an error, which leads many beginners to misuse it by writing the top-level declaration once instead of putting it in each function that needs it. It seems like this should be a syntax error, like using return outside a function or break outside a loop. Do these declarations serve any purpose, which would explain why it'

Comments

Sign in or sign up to comment and upvote.

No comments yet.