Why learners find classes and objects confusing
Posted on Tue 05 August 2025 in Software
One reason why Python is such a popular and powerful language is that it is straightforward to learn and make good progress. Learners can quickly become comfortable with many aspects of the language, such as control flow and functions.
The first obstacle that many learners come across is when they come across classes and objects. This is usually met as an intermediate subject several chapters into a book, by which time they may already have used objects, if they have been using libraries such as pandas.
Learners often find classes and objects difficult because introductory courses usually introduce them using toy examples that do not motivate their use: often the same effect can be achieved using dictionaries (for fields) and functions (for methods). The true utility of classes comes when working on a complex project where modularity and abstraction are useful for simplifying problems.
Classes play several roles
Grouping data
Classes and objects can be used to group related variables into a "compound" object:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
joanna = Person(name="Joanna", age=32)
The dataclass
feature (introduced in Python 3.7 and now standard)
provides a shortcut for constructing many such classes (inter alia,
creating the __init__
method automatically):
@dataclass
class Person:
name: str
age: int
Grouping data: One advantage is that a single argument can be passed around between functions:
# this
def describe(p: Person):
print(f"{p.name} is {p.age} years old")
describe(joanna)
This is neater than:
# not this
def describe(name: str, age: int):
print(f"{name} is {age} years old")
describe("Joanna", 32)
If we desire to start modelling new attributes of people and including
them when printing descriptions, we simply need to update the Person
class and the body of the describe
function, rather than the
signature of the describe
function.
Alternative: a dictionary: A similar effect might be achieved by using a dictionary:
joanna = {"name": "Joanna", "age": 32}
def describe(p: dict):
print(f"{p["name"]} is {p["age"]} years old")
So what is a class?
Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state. Class instances can also have methods (defined by its class) for modifying its state. From the Python Tutorial on classes.
In other words, a class is a compound data type that contains multiple fields or attributes (which are roughly synonymous, although there is a slight difference in Python), as well as associated methods and functions.
An object is what it does
In Mathematics: A Very Short Introduction, mathematician Timothy Gowers writes:
A mathematical object is what it does. (p. 18) ... If one learns to think abstractly, then many philosophical difficulties disappear.
This comes in the context of discussions over the nature of mathematical objects. Although philosophers have debated the 'intensive' properties of mathematical objects ('Do real numbers exist if you aren't able to construct them?') and mathematicians have attempted to formalise concepts in terms of logic and set theory ('How do you define the real numbers in terms of sets?'); Gowers, like most other mathematicians, takes the more practical view that most interesting questions about mathematical operations — such as how to do arithmetic, solve equations and perform analytical operations like taking limits — are about their 'extensive' properties
Recommended reading
Baeldung's tutorial on Object-Oriented-Programming Concepts in Java is a good piece that introduces the ideas of abstraction, encapsulation, inheritance and polymorphism. These design principles apply just as much to Python programming. The syntax in Java is a little different, but the examples there should be reasonably comprehensible; see their article on Java Classes and Objects for details.