| Start |
|---|
| Nonlocal Variables Python 3 added a new keyword called nonlocal. The nonlocal keyword adds a scope override to the inner scope. You can read all about it in PEP 3104 (opens new window). This is best illustrated with a couple of code examples. One of the most common examples is to create function that can increment: If you try running this code, you will receive an UnboundLocalError because the num variable is referenced before it is assigned in the innermost function. Let's add nonlocal to the mix: Basically (Note that the functionality presented here is better implemented using generators.) |
| Nonlocal Variables Python 3 added a new keyword called nonlocal. The nonlocal keyword adds a scope override to the inner scope. You can read all about it in PEP 3104 (opens new window). This is best illustrated with a couple of code examples. One of the most common examples is to create function that can increment: If you try running this code, you will receive an UnboundLocalError because the num variable is referenced before it is assigned in the innermost function. Let's add nonlocal to the mix: Basically (Note that the functionality presented here is better implemented using generators.) |
| Global Variables In Python, variables inside functions are considered local if and only if they appear in the left side of an assignment statement, or some other binding occurrence; otherwise such a binding is looked up in enclosing functions, up to the global scope. This is true even if the assignment statement is never executed. Normally, an assignment inside a scope will shadow any outer variables of the same name: Declaring a name The To summarize: in order to know whether a variable
|
Binding Occurrence Each of the above statements is a binding occurrence - |
| Functions skip class scope when looking up names Classes have a local scope during definition, but functions inside the class do not use that scope when looking up names. Because lambdas are functions, and comprehensions are implemented using function scope, this can lead to some surprising behavior. Users unfamiliar with how this scope works might expect From PEP 227 (opens new window): Names in class scope are not accessible. Names are resolved in the innermost enclosing function scope. If a class definition occurs in a chain of nested scopes, the resolution process skips class definitions. From Python's documentation on naming and binding (opens new window): The scope of names defined in a class block is limited to the class block; it does not extend to the code blocks of methods – this includes comprehensions and generator expressions since they are implemented using a function scope. This means that the following will fail: This example uses references from this answer (opens new window) by Martijn Pieters, which contains more in depth analysis of this behavior. |
| The del command This command has several related yet distinct forms. |
Local vs Global Scope What are local and global scope?All Python variabes which are accessible at some point in code are either in local scope or in global scope. The explanation is that local scope includes all variables defined in the current function and global scope includes variabled defined outside of the current function. One can inspect which variables are in which scope. Built-in functions What happens with name clashes?To modify a global variable, use keyword The scope is defined for the whole body of the function! What it means is that a variable will never be global for a half of the function and local afterwards, or vice-versa. Likewise, the oposite: Functions within functionsThere may be many levels of functions nested within functions, but within any one function there is only one local scope for that function and the global scope. There are no intermediate scopes. |
Read Article
https://develop.horje.com/learn/1434/reference