Read a Small Class

Follow a class definition, constructor call, instance fields, self, a small method, two independent instances, and one alias.

Library documentation often says that a call “returns an instance” or that an object “has an attribute.” A small class makes those statements concrete without requiring us to design a large object system.

The measurement report needs to keep three related results together: the accepted count, the mean, and its unit.

Read the Class as a Definition

This class defines that small result object:

A class defines a kind of object. The indented functions belong to that class. Calling the class creates an instance, one object made from the definition:

summary = Summary(5, 21.3, "°C")

Python creates a new Summary instance and calls __init__ to give it its starting state. __init__ is called an initializer. The whole expression Summary(5, 21.3, "°C") is commonly called a constructor call.

Inside these methods, self refers to the instance involved in the current call. During this constructor call, the three assignments produce:

StatementAttribute stored on summary
self.count = countsummary.count becomes 5
self.mean = meansummary.mean becomes 21.3
self.unit = unitsummary.unit becomes "°C"

The parameter names on the right receive the arguments. The attribute names on the left belong to the new instance.

Q1. Trace constructor arguments into attributes

After summary = Summary(5, 21.3, "°C"), what value does summary.mean contain?

Compute it first, then check your number.

HintFollow one assignment

In self.mean = mean, the right-side parameter received the second constructor argument.

SolutionThe mean attribute is 21.3

The argument 21.3 is assigned to the parameter mean. The initializer then stores it as summary.mean, so the attribute contains 21.3.

Not attempted
Review

Not marked done.

Trace One Method Call

Now call the method selected from that instance:

5 readings, mean 21.3 °C

For this call, self refers to summary. We can trace the expression without guessing:

Expression inside the methodValue read for this call
self.count5
self.mean21.3
self.unit"°C"
returned string"5 readings, mean 21.3 °C"

The method does not receive summary inside the visible parentheses. Selecting it through summary.report_line already associates the method with that instance; the call supplies that instance as self.

Q2. Bind self to the calling instance

Given the two instances below, what does second.report_line() return?

Choose one

Select one choice, then check.

HintRead from the object before the dot

In second.report_line(), the method reads attributes from second, not from first.

SolutionUse the second instance's fields

During second.report_line(), self refers to second. The method returns "2 readings, mean 18.75 °C".

Not attempted
Review

Not marked done.

Separate Instances, Then Follow an Alias

Each constructor call above created a different instance. Updating one does not change the other:

5 readings, mean 21.3 degrees C
2 readings, mean 18.75 °C

An assignment can instead give the same instance another name:

°C
°C

alias = first does not call Summary and does not create an instance. Both names refer to the first object, so a change through either name is visible through the other.

NameInstanceCurrent fields
firstinstance A5, 21.3, "°C"
aliasinstance A5, 21.3, "°C"
secondinstance B2, 18.75, "°C"

Q3. Complete and trace the Summary class

Replace the marked lines so each instance stores its own values and report_line returns the required text. The alias should update only the first instance.

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintKeep every field on self

Assign count, mean, and unit to matching attributes. In the method, build the returned string from self.count, self.mean, and self.unit.

SolutionStore and report the instance fields

The two constructor calls create separate instances. alias = first creates a second name for the first instance rather than copying it.

Not attempted
Review

Not marked done.

A class defines how an instance is initialized and what methods can read from it. Separate constructor calls create separate field values; a plain assignment can make an alias to one existing instance. Summary also exposes repetition: each plain record field is named in the initializer parameter and again in an assignment. A dataclass can generate that routine setup while a specific method such as report_line remains explicit. Before using one, the next lesson introduces the type-hint notation used to declare its fields.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 3 exercises marked done

Review

Not marked done.