A class is a blueprint for objects
A class in Python is a blueprint that bundles data with the functions that work on that data. An object is one thing built from the blueprint. Classes let your code match the problem: a log line, a basket, a user. This is the start of object-oriented programming, and every pattern later in this module builds on it.
By the end of this lesson
Note
I can model a thing as a class with state and behaviour.
Key words
| Word | What it means | Example |
|---|---|---|
| Class | a blueprint for making objects | |
| Object | one thing made from a class | |
| Attribute | a value stored inside an object | self.level |
| Method | a function that belongs to a class | is_error |
| Constructor | the method that sets up a new object | __init__ |
| State | what an object's attributes hold right now |
The cells students fill in are left blank in this preview.
One class, many objects
The lab keeps a log at /var/log/test.log. Each line has a date, a time, a level such as INFO or ERROR, and a message. A log line has data and things you do with it, so it makes a good class.
The class describes every log line. Each object is one real line. You write the class once and make as many objects as you need.
Read the level of a real error line
grep -m 1 ERROR /var/log/test.log | cut -d" " -f3Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A LogLine class in Python
class LogLine:
def __init__(self, level, message):
self.level = level
self.message = message
self.read = False
def is_error(self):
return self.level == "ERROR"
def short(self):
return self.level + ": " + self.message
def mark_read(self):
self.read = TrueThe constructor sets up each new object
The Run printed ERROR: the third word of that line is its level. The class above keeps level and message as attributes.
__init__ is the constructor. Python calls it when you write LogLine("ERROR", "disk full"). self is the object being built, so self.level = level stores the value inside that object. Every method takes self first, which is how it reaches that object's attributes.
Make a folder for this lesson
mkdir -p ~/oop1Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Go into it
cd ~/oop1Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Write the class to a file
printf 'class LogLine:\n def __init__(self, level, message):\n self.level = level\n self.message = message\n self.read = False\n\n def is_error(self):\n return self.level == "ERROR"\n\n def short(self):\n return self.level + ": " + self.message\n\n def mark_read(self):\n self.read = True\n' > loglines.pyShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Make two objects and call their methods
python3 -c "from loglines import LogLine; a = LogLine('ERROR', 'disk full'); b = LogLine('INFO', 'started'); print(a.short()); print(b.short()); print(a.is_error(), b.is_error())"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Each object keeps its own state
State is what an object's attributes hold now. A method can change it. mark_read sets self.read to True for that one object only.
Two objects from one class share the same methods and keep separate attributes. Changing one never changes the other.
Worked example: two objects, one change
| Moment | a.read | b.read |
|---|---|---|
| Both just made | False | False |
| After a.mark_read() | True | False |
| After b.mark_read() | True | True |
The cells students fill in are left blank in this preview.
Change one object and check the other
python3 -c "from loglines import LogLine; a = LogLine('INFO', 'started'); b = LogLine('INFO', 'started'); a.mark_read(); print(a.read, b.read)"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A common mistake
Warning
Many people think the class and the object are the same thing. The class is the blueprint and holds no log line itself. Write LogLine(...) to make an object, then call methods on that object, never on the class.