Skip to main content
← Python Study Guide

Unit 4 · Moderate

Object-Oriented Python

Classes in a language with no enforced privacy. Encapsulation here is a convention the programmer upholds rather than a rule the language imposes, which changes how it must be taught.

What a strong answer looks like

A strong Unit 4 answer distinguishes state that belongs to an instance from state that belongs to the class, and says which methods are allowed to change it.

Topics in this unit

1

Classes and Instances

Know

A class is a template; an instance is one object built from it. The initialiser runs at construction and sets up instance state.

Apply

Give every attribute a value in the initialiser so no instance starts half-built.

Watch out

Forgetting the instance parameter on a method, which produces a confusing argument-count error.

Study move

Write a class with two attributes and construct three instances with different values.

2

Instance Versus Class Attributes

Know

An attribute set on the class is shared by every instance; one set in the initialiser belongs to that instance alone.

Apply

Put per-object data in the initialiser and shared constants on the class.

Watch out

A mutable class attribute being modified through one instance and appearing changed for all of them.

Study move

Create a class with a mutable class attribute, modify it via one instance, and observe the effect on another.

3

Methods and Encapsulation

Know

Python marks intended privacy by naming convention rather than enforcement, so encapsulation is a contract between programmers.

Apply

Expose behavior through methods and treat underscore-prefixed names as internal.

Watch out

Assuming a leading underscore prevents access. It signals intent only.

Study move

Write a class whose internal representation could change without breaking callers who respect the convention.

4

Special Methods

Know

Defining the string representation and equality methods makes an object print usefully and compare sensibly.

Apply

Implement a readable representation for any class you will debug.

Watch out

Comparing two objects without defining equality, which compares identity and returns false for equal values.

Study move

Define equality on a small class and show two distinct instances comparing equal.

Emphasized in this unit

Connections and techniques that receive extra attention in this unit.

  • Initialising every attribute at construction
  • Distinguishing class from instance state
  • Treating privacy as a convention to uphold

Varies by course

Related topics some schools attach to this unit and others leave out. Covered on request rather than assumed.

  • Inheritance. Depth varies widely; some sections stop at single classes.
  • Dataclasses. Used in some courses to reduce boilerplate.

Mastery checklist

  • Write a class whose instances are fully initialised.
  • Explain the difference between class and instance attributes.
  • Say what an underscore prefix does and does not do.
  • Define a useful string representation.

Check yourself

  • Why does a mutable class attribute surprise people?
  • What does Python actually enforce about private attributes?
  • Why do two equal-looking objects compare unequal by default?

Modeling drill

Write a class representing a quiz result that prints readably and compares equal when the scores match, and say which attributes belong to the class rather than the instance.

ClassInstanceInitialiserInstance attributeClass attributeSpecial methodConvention