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 5 added one line
%%
At line 5 changed one line
We can define __classes__ in Python. Classes are defined globally, within the file/module as they begin in column 0.
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 7 changed one line
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 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 9 changed 3 lines
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
%%filename main.py %%
At line 13 changed one line
class MotorController:
\\# 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 15 changed 5 lines
def \\__init__(self):
self.port_forward_motor = Motor('pfwd')
self.stbd_forward_motor = Motor('sfwd')
print('motor controller ready.')
%%
try:
At line 21 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 call or execute the function here
display_message('hello world.')
At line 33 changed 2 lines
def get_speed(self):
return self.motor_speed
except Exception as e:
print('an error occurred: {}'.format(e))
finally:
print('complete.')
At line 28 added 4 lines
----
__Previous:__ [MicroPython Tutorial Exercise 2|MicroPythonTutorialExercise02]: blocks \\
__Next:__ [MicroPython Tutorial Exercise 4|MicroPythonTutorialExercise04]: classes