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 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.
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.
%%filename main.py %%
%%python
\\# we define a function called "display_message"
def display_message(message):
print(message)
raise Exception('we did something wrong.')
At line 9 removed 39 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
%%python {{{
from motor import Motor
class MotorController:
def __init__(self):
self.port_forward_motor = Motor('pfwd')
self.stbd_forward_motor = Motor('sfwd')
print('motor controller ready.')
}}}
%%
!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))
def get_speed(self):
return self.motor_speed
}}}
%%
!main.py:
%%python {{{
from motor_controller import MotorController
At line 49 changed 6 lines
motor_ctrl = MotorController()
except Exception as ex:
print('an error occurred: {}'.format(ex))
display_message('hello world.')
except Exception as e:
print('an error occurred: {}'.format(e))
At line 57 removed one line
}}}
At line 61 changed 2 lines
__Previous:__ [MicroPython Tutorial Exercise 2|MicroPythonTutorialExercise02]: functions \\
__Next:__ [MicroPython Tutorial Exercise 4|MicroPythonTutorialExercise04]: using classes
__Previous:__ [MicroPython Tutorial Exercise 2|MicroPythonTutorialExercise02]: blocks \\
__Next:__ [MicroPython Tutorial Exercise 4|MicroPythonTutorialExercise04]: classes