ATHENA
Python for Intermediate Learners / Smart Reader
THE ATHENA LIBRARY

Python for Intermediate Learners

Daniel Haile · textbook

Chapter 4 · Classes in Python

Defining your own types

In Python, class Student: begins a new type. The __init__ method runs when an object is created, and self refers to the instance being built. Attributes are attached to self rather than declared in advance.

Because Python is dynamically typed, classes feel lighter than in Java. Yet the underlying idea is identical: a class bundles data with the functions that operate on it, and each instance has its own state.

Dunder methods such as __repr__ and __eq__ let your objects cooperate with built-in operations. Implementing them is how a class begins to feel like a native part of the language.

Fictional educational sample written for the Athena demo · Page 74 of 356.

25%saved