Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 62e6d30

Browse files
Added info about Nonlocal NameSpace/Variable PyBasics-1.ipynb
1 parent a8a7c5e commit 62e6d30

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

‎basic/PyBasics-1.ipynb

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@
301301
"<!-- using Table Style for this -->\n",
302302
"| *1. Arithmetic operators* | \n",
303303
"|:--:| \n",
304-
"| ![Arithmetic](imgs/arithmetic_ops.PNG)|\n",
304+
"| ![Arithmetic](imgs/arithmetic_ops.PNG)|\n",
305305
"\n",
306306
"\n",
307307
"| *2. Assignment Operators* | \n",
@@ -415,10 +415,16 @@
415415
"- These different namespaces are isolated. Hence, the same name that may exist in different modules does not collide.\n",
416416
"- Modules can have various functions and classes. \n",
417417
"\n",
418-
"`Local namespace`\n",
418+
"**`Local namespace`**\n",
419419
"- A local namespace is created when a function is called, which has all the names defined in it.\n",
420420
"- Similar is the case with class.\n",
421421
"\n",
422+
"**`Python Nonlocal Variables`**\n",
423+
"\n",
424+
"- In Python, nonlocal variables are used in nested functions whose local scope is not defined. \n",
425+
" - This means that the variable can be neither in the local nor the global scope.\n",
426+
"- We use the nonlocal keyword to create nonlocal variables.\n",
427+
"\n",
422428
"## <a id='toc6_1_'></a>[Python Variable Scope << Namespaces >>](#toc0_)\n",
423429
"**When a reference is made inside a function, the name is searched in the local namespace, then in the global namespace and finally in the built-in namespace.**"
424430
]
@@ -463,6 +469,41 @@
463469
"outer_function()"
464470
]
465471
},
472+
{
473+
"cell_type": "code",
474+
"execution_count": 1,
475+
"metadata": {},
476+
"outputs": [
477+
{
478+
"name": "stdout",
479+
"output_type": "stream",
480+
"text": [
481+
"inner: nonlocal\n",
482+
"outer: nonlocal\n"
483+
]
484+
}
485+
],
486+
"source": [
487+
"##################### Nonlocal NameSpace #######\n",
488+
"# outside function \n",
489+
"def outer():\n",
490+
" message = 'local'\n",
491+
"\n",
492+
" # nested function \n",
493+
" def inner():\n",
494+
"\n",
495+
" # declare nonlocal variable\n",
496+
" nonlocal message\n",
497+
"\n",
498+
" message = 'nonlocal'\n",
499+
" print(\"inner:\", message)\n",
500+
"\n",
501+
" inner()\n",
502+
" print(\"outer:\", message)\n",
503+
"\n",
504+
"outer()"
505+
]
506+
},
466507
{
467508
"cell_type": "markdown",
468509
"metadata": {},

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /