Skip to content
Tactiverge

Learning Rooms/○ Level 3 · Innovator/Computing and algorithms

Python OOP Tutorial 1: Classes and Instances

Corey Schafer · Length: see YouTube · captions on YouTube · nothing loads until you press play

By the end you can…

  • Define a class with __init__, attributes and methods to model a sensor.
  • Create several instances and explain why each keeps its own attribute values.
  • Tell instance attributes from class attributes and avoid the shared-list bug.
?Predict▶Watch✓Practise⚙Build

Watch it all: no segment has been checked by hand for this video yet.

1.Predict before you press play

A class puts readings = [] on the class body instead of inside __init__. You record 50 cm on the front sensor. What does the rear sensor's readings list contain?

Pick one before you press play. A wrong guess is fine: it is what makes the video stick.

2.See the idea first

Play with this diagram for a minute. It gives you something to hang the video on.

Loading the interactive…

Can't use the diagram? Read a description instead

The inspector shows a RangeSensor class with a front sensor (offset 2 cm) and a rear sensor (offset −1 cm). Recording 50 cm on the front stores 52 in front's own list only. With the bug toggle on, readings live on the class, so the same 52 appears in both sensors' readings.

3.Watch, actively

Builds a class step by step, so you can see what __init__ and self actually do for each new object.

Length: see YouTube · captions on YouTube · source

Nothing loads from YouTube until you press play.

Watch for…

  • → Watch what self refers to inside __init__, and how each new instance gets its own attribute values.

Pause whenever you like. Rewatching a hard minute is a good habit, not a failure.

Can't stream it? Read the idea instead

A class is a blueprint and each instance is one object built from it. The __init__ method sets up each new object's own data, and self is how methods reach that object's data. Ten sensors can share one class while each keeps its own name and calibration.

What the video covers (our own note, written when we verified it): Builds a class step by step, so you can see what __init__ and self actually do for each new object.

Written by Tactiverge in our own words, not a transcript. The full lesson below explains the idea without the video.

Read it instead

Jot down one thing that surprised you and one question you still have.

Private: saved only on this device, never sent to Tactiverge.

4.Practise what you just saw

These come from the lesson that use this video and are checked on the server. Wrong answers get a hint and another try.

Practice 1

Which class definition gives each sensor its own readings list?

Practice 2

RangeSensor.unit = "cm" is a class attribute. After front = RangeSensor('front', 2), what is front.unit?

Practice 3

rear = RangeSensor('rear', -1); rear.mean() is called before any readings. With the worked example's code, what happens?

Practice 4

Using RangeSensor from the worked example, s = RangeSensor('left', 3); s.record(40); s.record(46). What does s.mean() return?

cm

5.Try it yourself

No simulation models this idea yet. The lesson below has an interactive diagram instead.

6.Where engineers use it

Robot drivers commonly define one class per device type (an IMU, a lidar, a motor controller) and create one object per physical device, each holding its own port, calibration and latest data.

From the applied view in the lesson One blueprint, many sensors. Checked by automated tests only.

7.Build something with it

Follow-up project

Write a Part class for a stores inventory with attributes part_no, description, mass_g and quantity, a method total_mass_kg() and a method withdraw(n) that refuses (raises ValueError) to go below zero. Create three parts and print the total mass of everything in stock.

Where this video lives

In the course catalogue: CSE-310 Structured programs and data

The video is by Corey Schafer, embedded from YouTube with privacy-enhanced mode and never autoplayed. The room around it (prompts, notes, practice, projects) is Tactiverge's, written with AI assistance and checked by automated tests only.