11Variables
22=========
33
4- Local, non-local and global variables
5- -------------------------------------
4+ Local variables
5+ ---------------
66
77Here you return to the definition of ``fact `` from the beginning of this
88:doc: `index ` chapter:
99
10- .. code-block :: pycon
10+ .. code-block :: python
1111
12- >>> def fact(n):
13- ... """Return the factorial of the given number."""
14- ... f = 1
15- ... while n > 0:
16- ... f = f * n
17- ... n = n - 1
18- ... return f
19- ...
12+ def fact (n ):
13+ """ Return the factorial of the given number."""
14+ f = 1
15+ while n > 0 :
16+ f = f * n
17+ n = n - 1
18+ return f
2019
2120 Both the variables ``f `` and ``n `` are local to a particular call to the
2221function ``fact ``; changes made to them during the execution of the function
2322have no effect on variables outside the function. All variables in the parameter
2423list of a function and all variables created within a function by an assignment,
25- such as ``f = 1 ``, are local to the function.
24+ such as ``f = 1 ``, are local to the function:
25+
26+ .. code-block :: pycon
27+
28+ >>> fact(3)
29+ 6
30+ >>> f
31+ Traceback (most recent call last):
32+ File "<python-input-27>", line 1, in <module>
33+ f
34+ NameError: name 'f' is not defined
35+ >>> n
36+ Traceback (most recent call last):
37+ File "<python-input-28>", line 1, in <module>
38+ n
39+ NameError: name 'n' is not defined
40+
41+ Global variables
42+ ----------------
2643
2744You can explicitly make a variable a global variable by declaring it with the
28- `` global `` statement before it is used. Global variables can be accessed and
29- changed by the function. They exist outside the function and can also be
30- accessed and changed by other functions that declare them as global, or by code
31- that is not inside a function. Here is an example that illustrates the
45+ :ref: ` global < python3:global >` statement before it is used. Global variables can
46+ be accessed and changed by the function. They exist outside the function and can
47+ also be accessed and changed by other functions that declare them as global, or
48+ by code that is not inside a function. Here is an example that illustrates the
3249difference between local and global variables:
3350
34- .. code-block :: pycon
51+ .. code-block :: python
3552
36- >>> def my_func():
37- ... global x
38- ... x = 1
39- ... y = 2
40- ...
53+ def my_func ():
54+ global x
55+ x = 1
56+ y = 2
4157
4258 .. code-block :: pycon
4359
@@ -59,20 +75,36 @@ true for ``y``; the local variable ``y`` inside ``my_func`` initially refers to
5975the same value as the variable ``y `` outside ``my_func ``, but the assignment
6076causes ``y `` to refer to a new value that is local to the ``my_func `` function.
6177
62- .. seealso ::
78+ Non-local variables
79+ -------------------
6380
64- * :ref: `python3:global `
81+ While :ref: `global <python3:global >` is used for a top-level variable,
82+ :ref: `nonlocal <python3:nonlocal >` refers to any variable in an enclosing area:
83+
84+ .. code-block :: python
85+
86+ def enclosing ():
87+ x = " Enclosing function variable"
88+
89+ def enclosed ():
90+ nonlocal x
91+ x = " Enclosed function variable"
92+
93+ enclosed()
94+ print (x)
95+
96+ .. code-block :: pycon
6597
66- While `` global `` is used for a top-level variable, `` nonlocal `` refers to any
67- variable in an enclosing area.
98+ >>> enclosing()
99+ Enclosed function variable
68100
69101 .. seealso ::
70102
71- * :ref: `python3:nonlocal `
72103 * :pep: `3104 `
73104
74105Checks
75106------
76107
77- * Assuming ``x = 1 ``, what value does ``x `` have after the execution of
78- ``func() `` and ``gfunc() ``?
108+ * Assuming ``x = 1 ``, :func: `func ` sets the local variable ``x `` to ``2 `` and
109+ :func: `gfunc ` sets the global variable ``x `` to ``3 ``, what value does ``x ``
110+ assume after :func: `func ` and :func: `gfunc ` have been run through?
0 commit comments