mirror of
https://github.com/rasbt/python_reference.git
synced 2024-11-27 22:11:13 +00:00
some typo fixes
This commit is contained in:
parent
a6777f450d
commit
1351a00436
|
@ -41,7 +41,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"This is a short tutorial about Python's scope resolution for variable names using the LEGB-rule. The following section will have short example code blocks that should illustrate the problem followed by short explanations. You can simply read it from start to end, but I'd recommend you to execute the code snippets yourself by copy & paste, or by directly [downloading this IPython notebook](https://raw.githubusercontent.com/rasbt/python_reference/master/tutorials/scope_resolution_legb_rule.ipynb)."
|
||||
"This is a short tutorial about Python's scope resolution for variable names using the LEGB-rule. The following section will provide short example code blocks that should illustrate the problem followed by short explanations. You can simply read this tutorial from start to end, but I'd like to ecourage you to execute the code snippets - you can either copy & paste them, or for your convenience, simply [download this IPython notebook](https://raw.githubusercontent.com/rasbt/python_reference/master/tutorials/scope_resolution_legb_rule.ipynb)."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -79,9 +79,9 @@
|
|||
"metadata": {},
|
||||
"source": [
|
||||
"## Objectives\n",
|
||||
"- Namespaces and scopes - Where does Python look for variable names?\n",
|
||||
"- What happens if the same variable name is defined (reused) multiple times?\n",
|
||||
"- In which order are namespaces being searched for variable names?"
|
||||
"- Namespaces and scopes - where does Python look for variable names?\n",
|
||||
"- Can we define/reuse variable names for multiple objects at the same time?\n",
|
||||
"- In which order does Python search different namespaces for variable names?"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -104,7 +104,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"A namespace represents a mapping from names to objects. E.g., if we want to access any Python object, we do it by the associated name. \n",
|
||||
"A namespace represents a mapping from names to objects, and we can access any object in Python by its associated name. E.g., if we create a list object `a_list = [1, 2, 3]`, we can modify this list by using its variable name `a_list`, which maps to the list object stored in the memory.\n",
|
||||
"We could picture a namespace as a simple dicitonary, e.g., \n",
|
||||
"`a_namespace = {'name_a':object_a, 'name_b':object_b, ...}` \n",
|
||||
"(which is also how namespaces are currently implemented in Python). \n",
|
||||
|
@ -119,7 +119,7 @@
|
|||
"For example, let's assume we want to access an object via its name `name_a`, in which namespace would Python look up the mapping?\n",
|
||||
"\n",
|
||||
"This is where the concept of **scope** comes into play. In Python, we have 4 different scopes: **L** ocal, **E** nclosed, **G** lobal, **B** uilt-in (LEGB). \n",
|
||||
"According to the LEGB-rule, Python looks for an object through the namespaces in the following order L -> E -> G -> B: If a variable name is not found in the local namespaces, the namespaces of the enclosed scope are being searched, and if the variable name is also not defined here, the global namespaces come next and so forth."
|
||||
"According to the LEGB-rule, Python searches the different namespace levels in the following order L -> E -> G -> B, i.e., if a particular name:object mapping cannot be found in the local namespaces, the namespaces of the enclosed scope are being searched next. If the search in the enclosed scope is unsuccessful, too, Python moves on to the global namespace, and eventually, it will search the global namespaces (side note: if a name cannot found in any of the namespaces, a `NameError` is raised)."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -143,7 +143,7 @@
|
|||
"metadata": {},
|
||||
"source": [
|
||||
"**Example 1.1** \n",
|
||||
"As a warm-up exercise, let us first forget about the enclosed (E) and built-in (B) scopes in the LEGB rule and take a look at LG - the local and global scope. \n",
|
||||
"As a warm-up exercise, let us first forget about the enclosed (E) and built-in (B) scopes in the LEGB rule and only take a look at LG - the local and global scopes. \n",
|
||||
"What does the following code print?"
|
||||
]
|
||||
},
|
||||
|
@ -195,7 +195,7 @@
|
|||
"source": [
|
||||
"### Here is why:\n",
|
||||
"\n",
|
||||
"We call `a_func()` first, which is supposed to print the value of `a_var`. According to the LEGB rule, the function will first look in its own local scope (L) if `a_var` is defined there. Since `a_func()` does not have its own `a_var`, it will look one-level above in the global scope (G) in which we defined `a_var` previously.\n",
|
||||
"We call `a_func()` first, which is supposed to print the value of `a_var`. According to the LEGB rule, the function will first look in its own local scope (L) if `a_var` is defined there. Since `a_func()` does not define its own `a_var`, it will look one-level above in the global scope (G) in which `a_var` has been defined previously.\n",
|
||||
"<br>\n",
|
||||
"<br>"
|
||||
]
|
||||
|
@ -415,7 +415,7 @@
|
|||
"source": [
|
||||
"### Here is why:\n",
|
||||
"\n",
|
||||
"Let us quickly recapitulate what we just did: We called `outer()`, which defines the variable `a_var` locally (next to an existing `a_var` in the global scope). The `outer()` function then calls `inner()`, which in turn defines a variable with the name `a_var` as well. The `print()` function inside `inner()` looks in the local scope first (L->E), before it goes up the scope hierarchy, and therefore prints the value that was assigned in the local scope."
|
||||
"Let us quickly recapitulate what we just did: We called `outer()`, which defined the variable `a_var` locally (next to an existing `a_var` in the global scope). Next, the `outer()` function called `inner()`, which in turn defined a variable with of name `a_var` as well. The `print()` function inside `inner()` searched in the local scope first (L->E) before it went up in the scope hierarchy, and therefore it printed the value that was assigned in the local scope."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -527,7 +527,7 @@
|
|||
"source": [
|
||||
"### Here is why:\n",
|
||||
"\n",
|
||||
"Since the exact same names can be used to map names to different objects - as long as the names are in different name spaces - there is no problem of reusing the name `len` to define our own length function (this is just for demonstration pruposes, it is NOT recommended). As we go up in Python's L -> E -> G -> B hierarchy, the function `a_func()` finds `len()` in the global scope first, before it attempts to search the namespaces in the built-in scope."
|
||||
"Since the exact same names can be used to map names to different objects - as long as the names are in different name spaces - there is no problem of reusing the name `len` to define our own length function (this is just for demonstration pruposes, it is NOT recommended). As we go up in Python's L -> E -> G -> B hierarchy, the function `a_func()` finds `len()` already in the global scope first before it attempts"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue
Block a user