What are the rules for local and global variables in Python?

Technology CommunityCategory: PythonWhat are the rules for local and global variables in Python?
VietMX Staff asked 3 years ago

In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a value anywhere within the function’s body, it’s assumed to be a local unless explicitly declared as global.

Requiring global for assigned variables provides a bar against unintended side-effects.