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

This page was created on 2025-07-30 02:56 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
14 2025-07-30 12:56 1 KB Murray Altheim to previous
13 2025-07-30 12:49 992 bytes Murray Altheim to previous | to last
12 2025-07-30 06:47 980 bytes Murray Altheim to previous | to last
11 2025-07-30 06:44 717 bytes Murray Altheim to previous | to last
10 2025-07-30 06:19 1 KB Murray Altheim to previous | to last
9 2025-07-30 04:12 1 KB Murray Altheim to previous | to last
8 2025-07-30 04:09 1 KB Murray Altheim to previous | to last
7 2025-07-30 04:06 1 KB Murray Altheim to previous | to last
6 2025-07-30 04:01 1 KB Murray Altheim to previous | to last
5 2025-07-30 04:00 1 KB Murray Altheim to previous | to last
4 2025-07-30 03:59 1 KB Murray Altheim to previous | to last
3 2025-07-30 02:59 1 KB Murray Altheim to previous | to last
2 2025-07-30 02:57 937 bytes Murray Altheim to previous | to last
1 2025-07-30 02:56 757 bytes Murray Altheim to last

Page References

Incoming links Outgoing links

Version management

Difference between version and

At line 1 changed one line
!!! Classes and Methods
!!! Functions
At line 3 added one line
%%info
At line 4 removed 16 lines
We can define __classes__ in Python. Classes are defined globally, within the file/module as they begin in column 0.
Within a class its functions are called __methods__. Unlike functions, methods are defined within the scope of the class and are therefore indented within the class's block.
A class has a constructor, a special method named {{{__init__}}}. All special methods in Python are surrounded by double underscores. (There are more of these but we'll get to them later.)
!motor_controller.py
%%python {{{
class MotorController:
def __init__(self):
self.port_forward_motor = Motor('pfwd')
self.stbd_forward_motor = Motor('sfwd')
print('motor controller ready.')
}}}
At line 22 changed 11 lines
!motor.py:
%%python {{{
class Motor:
def __init__(self, name):
self.motor_name = name
self.motor_speed = 0
print('motor {} ready.'.format(self.motor_name))
def set_speed(self, speed):
self.motor_speed = speed
print("speed: {}".format(speed))
We can define __functions__ in Python. Functions are defined globally, within the file/module as they begin in ''column 0'' (the left-most position on a line, with no indentation).
At line 34 changed 4 lines
def get_speed(self):
return self.motor_speed
}}}
%%
A function is a block that defines a series of lines we want to execute when the function is called. We can pass zero or more __arguments__ (variables) to the function.
At line 11 added 6 lines
%%filename main.py %%
%%python
\\# we define a function called "display_message", passing a single argument named "message"
def display_message(message):
print(message)
raise Exception('we did something wrong.')
At line 40 removed 2 lines
!main.py:
%%python {{{
At line 20 added 2 lines
# we call or execute the function here
display_message('hello world.')
At line 45 changed 5 lines
except Exception as ex:
print('an error occurred: {}'.format(ex))
except Exception as e:
print('an error occurred: {}'.format(e))
At line 52 removed one line
}}}
At line 29 added 3 lines
----
__Previous:__ [MicroPython Tutorial Exercise 2|MicroPythonTutorialExercise02]: blocks \\
__Next:__ [MicroPython Tutorial Exercise 4|MicroPythonTutorialExercise04]: classes