Micro Python Tutorial Exercise 03
Back to current versionRestore this version

Classes and Methods#

This is Exercise 3 of the Micro Python Tutorial.

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.)

class Motor:

def init(self, name): self.motor_name = name self.motor_speed = 0 print("motor {} ready.".format(self.motor_name))