Skip to content

Commit 11643f1

Browse files
chavidldko
andauthored
Episode 8, functions : about reading a global variable inside a function (#942)
* First proposal * Remove the global trap explanation from the section about variable scope, and add a sentence in the dedicated challenge. * Remove dummy sentence extract. * Move the debate about the local/global trap in the challenge. * Second review * Wrap line to keep under 100 characters Co-authored-by: Lauren Ko <[email protected]>
1 parent 1d0cdb0 commit 11643f1

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

_episodes/08-func.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ keypoints:
1818
- "Call a function using `function_name(value)`."
1919
- "Numbers are stored as integers or floating-point numbers."
2020
- "Variables defined within a function can only be seen and used within the body of the function."
21-
- "If a variable is not defined within the function it is used,
22-
Python looks for a definition before the function call"
21+
- "Variables created outside of any function are called global variables."
22+
- "Within a function, we can access global variables."
23+
- "Variables created within a function override global variables if their names match."
2324
- "Use `help(thing)` to view help for something."
2425
- "Put docstrings in functions to provide help for that function."
2526
- "Specify default values for parameters when defining a function using `name=value`
@@ -177,6 +178,29 @@ temperature in Kelvin was: 373.15
177178
~~~
178179
{: .output}
179180

181+
The variable `temp_kelvin`, being defined outside any function,
182+
is said to be [global]({{ page.root }}/reference.html#global-variable).
183+
184+
Inside a function, one can read the value of such global variables:
185+
~~~
186+
def print_temperatures():
187+
print('temperature in Fahrenheit was:', temp_fahr)
188+
print('temperature in Kelvin was:', temp_kelvin)
189+
190+
temp_fahr = 212.0
191+
temp_kelvin = fahr_to_kelvin(temp_fahr)
192+
193+
print_temperatures()
194+
~~~
195+
{: .language-python}
196+
197+
~~~
198+
temperature in Fahrenheit was: 212.0
199+
temperature in Kelvin was: 373.15
200+
~~~
201+
{: .output}
202+
203+
180204
## Tidying up
181205

182206
Now that we know how to wrap bits of code up in functions,
@@ -872,6 +896,9 @@ readable code!
872896
> > `k`. The function does not return any values
873897
> > and does not alter `k` outside of its local copy.
874898
> > Therefore the original value of `k` remains unchanged.
899+
> > Beware that a local `k` is created because `f2k` internal statements
900+
> > *affect* a new value to it. If `k` was only `read`, it would simply retreive the
901+
> > global `k` value.
875902
> {: .solution}
876903
{: .challenge}
877904

reference.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ function
111111
function call
112112
: A use of a function in another piece of software.
113113

114+
global variable
115+
: A variable defined outside of a function. It can be used in global statements,
116+
and read inside functions.
117+
114118
heat map
115119
: A graphical representation of two-dimensional data in which colors,
116120
ranging on a scale of hue or intensity, represent the data values.

0 commit comments

Comments
 (0)