Computer >> Computer tutorials >  >> Programming >> Python

What are the basic scoping rules for python variables?


In the book Learning Python by Mark Lutz, he suggests the following mnemonic for remember how Python scoping works: LEGB

Going from the narrowest scope to the widest scope:

L stands for “Local”. It refers to variables that are defined in the local scope of functions.

E stands for “Enclosing”. It refers to variables defined in the local scope of functions wrapping other functions. G stands for “Global”. These are the variables defined at the top level of files and modules.

B stands for “Built in”. These are the names that are loaded into scope when the interpreter starts up.