This page (revision-9) was last changed on 2025-07-30 12:47 by Murray Altheim

This page was created on 2025-07-30 10:58 by Murray Altheim

Only authorized users are allowed to rename pages.

Only authorized users are allowed to delete pages.

Page revision history

Version Date Modified Size Author Changes ... Change note
9 2025-07-30 12:47 3 KB Murray Altheim to previous
8 2025-07-30 12:09 3 KB Murray Altheim to previous | to last
7 2025-07-30 12:07 3 KB Murray Altheim to previous | to last
6 2025-07-30 12:03 3 KB Murray Altheim to previous | to last
5 2025-07-30 11:53 3 KB Murray Altheim to previous | to last
4 2025-07-30 11:52 3 KB Murray Altheim to previous | to last
3 2025-07-30 11:47 3 KB Murray Altheim to previous | to last
2 2025-07-30 11:14 3 KB Murray Altheim to previous | to last
1 2025-07-30 10:58 1 KB Murray Altheim to last

Page References

Incoming links Outgoing links

Version management

Difference between version and

At line 3 changed 2 lines
When you type a __name__ its definition (and therefore its meaning and usage) as used by the Python interpreter depends on where it is defined.
To determine where a name is defined, Python
%%info
This is an additional exercise of the [MicroPython Tutorial|MicroPythonTutorial].
%%
At line 6 changed one line
depends on
When you type a __name__ its definition (and therefore its meaning and usage) as used by the Python interpreter
''depends on where it is defined''.
At line 12 removed one line
* __function inside function__:
At line 17 changed one line
[{TableOfContents}]
[{TableOfContents title='Contents'}]
At line 47 added 4 lines
If you only ever expect to use a function within an existing function, yes, you can define
a function within another function. This is called a __nested function__. The scope of names
is therefore determined by where they are defined.
At line 71 added one line
!! Global Scope
At line 73 added 58 lines
These are names defined at the top level (i.e., starting at column 0) of a module,
or declared global with the {{global}} keyword.
%%filename global_scope.py %%
%%python {{{
# example of global scope
x = "I'm global"
def show_global():
print(x) # read-only access to the global value of x
show_global()
}}} %%
If you want to be able to modify a global variable, you can use the {{global}} keyword.
This is a safety feature so that you don't accidentally modify a global variable within a non-global scope unless you explicitly use the {{global}} keyword.
%%filename global_keyword.py %%
%%python {{{
# example of the global keyword
# we define a global variable called 'count'
count = 0
def increment_by_2():
global count # we use the 'global' keyword so we can modify x
count += 2
# we call the increment_by_2() function inside a for loop
for i in range(0,5):
increment_by_2()
print("i={}; count: {}".format(i, count))
}}} %%
When this code is executed, we can see the value of {{i}} being incremented by the {{for}} loop, as well as
the value of the global variable {{count}} being incremented within the {{increment_by_2()}} function.
%%repl {{{
/pyboard> repl
Entering REPL. Use Control-X to exit.
>
MicroPython v1.25.0 on 2025-04-15; Raspberry Pi Pico with RP2040
Type "help()" for more information.
>>>
>>> import global_keyword
i=0; count: 2
i=1; count: 4
i=2; count: 6
i=3; count: 8
i=4; count: 10
>>>
}}} %%