mirror of
https://github.com/rasbt/python_reference.git
synced 2025-03-15 10:09:54 +00:00
fixed for-loop example
This commit is contained in:
parent
bea168e07b
commit
dd25efd41f
@ -176,7 +176,7 @@
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"prompt_number": 4
|
"prompt_number": 1
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -200,6 +200,47 @@
|
|||||||
"So, how does Python now which namespace it has to search if we want to print the value of the variable `i`? This is where Python's LEGB-rule comes into play, which we will discuss in the next section."
|
"So, how does Python now which namespace it has to search if we want to print the value of the variable `i`? This is where Python's LEGB-rule comes into play, which we will discuss in the next section."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Tip:\n",
|
||||||
|
"If we want to print out the dictionary mapping of the global and local variables, we can use the\n",
|
||||||
|
"the functions `global()` and `local()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"collapsed": false,
|
||||||
|
"input": [
|
||||||
|
"#print(globals()) # prints global namespace\n",
|
||||||
|
"#print(locals()) # prints local namespace\n",
|
||||||
|
"\n",
|
||||||
|
"glob = 1\n",
|
||||||
|
"\n",
|
||||||
|
"def foo():\n",
|
||||||
|
" loc = 5\n",
|
||||||
|
" print('loc in foo():', 'loc' in locals())\n",
|
||||||
|
"\n",
|
||||||
|
"foo()\n",
|
||||||
|
"print('loc in global:', 'loc' in globals()) \n",
|
||||||
|
"print('glob in global:', 'foo' in globals())"
|
||||||
|
],
|
||||||
|
"language": "python",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"output_type": "stream",
|
||||||
|
"stream": "stdout",
|
||||||
|
"text": [
|
||||||
|
"loc in foo(): True\n",
|
||||||
|
"loc in global: False\n",
|
||||||
|
"glob in global: True\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"prompt_number": 11
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
@ -490,26 +531,6 @@
|
|||||||
"<br>"
|
"<br>"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"### Tip:\n",
|
|
||||||
"If we want to print out the dictionary mapping of the global and local variables, we can use the\n",
|
|
||||||
"the functions `global()` and `local()"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"collapsed": false,
|
|
||||||
"input": [
|
|
||||||
"print(globals()) # prints global namespace\n",
|
|
||||||
"print(locals()) # prints local namespace"
|
|
||||||
],
|
|
||||||
"language": "python",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": []
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user