Skip to main content
Classes provide a way to bundle data and functionality together. Python’s class mechanism is powerful yet straightforward, supporting all standard features of object-oriented programming.

A Word About Names and Objects

In Python, objects have individuality, and multiple names can be bound to the same object (aliasing):
This is important for mutable objects like lists and dictionaries. For immutable types (numbers, strings, tuples), aliasing doesn’t affect program behavior.

Python Scopes and Namespaces

A namespace is a mapping from names to objects. Examples include:
  • Built-in names (functions like abs(), exception names)
  • Global names in a module
  • Local names in a function
  • Attributes of an object
A scope is a textual region where a namespace is directly accessible.

Scope Example

Output:

A First Look at Classes

Class Definition Syntax

The simplest class definition:

Class Objects

Classes support two operations: attribute references and instantiation.
Attribute references:
Instantiation:

The init Method

Customize instance creation with __init__():

Instance Objects

Instances understand two kinds of attributes:
  1. Data attributes (instance variables)
  2. Methods (functions that belong to the object)

Method Objects

Methods are called on instances:
You can also store method references:
What happens behind the scenes:
The instance is automatically passed as the first argument.

Class and Instance Variables

Class variables are shared by all instances:
Shared mutable objects can cause problems:
Correct design uses instance variables:

Inheritance

Derive new classes from existing ones:
Example:

Calling Base Class Methods

Built-in Functions for Inheritance

isinstance(): Check an instance’s type:
issubclass(): Check class inheritance:

Multiple Inheritance

Python supports multiple base classes:
Attribute search is depth-first, left-to-right:

Private Variables

Python has no true private variables, but there’s a convention: Single underscore (_spam): Internal implementation detail
Name mangling (__spam): Avoid name clashes in subclasses
Name mangling replaces __spam with _classname__spam to avoid conflicts.

Iterators

Make your classes iterable:
Usage:

Generators

Generators are a simple way to create iterators:
Key features:
  • __iter__() and __next__() are created automatically
  • Local variables and execution state are saved between calls
  • Automatically raise StopIteration when done

Generator Expressions

Like list comprehensions but with parentheses:

Dataclasses

Use dataclasses for simple data containers:
Dataclasses automatically generate __init__(), __repr__(), and other methods.

Best Practices

Use self

Always use self as the first parameter name for instance methods

Document Classes

Use docstrings to document class purpose and usage

Favor Composition

Prefer composition over inheritance when possible

Keep It Simple

Don’t over-engineer - start simple and refactor as needed

Summary

You’ve completed the Python tutorial! You now understand:
  • Classes and objects
  • Inheritance and polymorphism
  • Special methods and protocols
  • Iterators and generators
  • Modern Python features like dataclasses
Continue learning by exploring:
  • The Python Standard Library
  • Advanced topics like decorators, context managers, and metaclasses
  • Real-world projects and contributions to open source
Happy coding!