Python Programming · Foundations
Classes and Objects
On this page 9 sections
In 30 seconds
A Python class An object created by a class statement that defines attributes and behavior for instances. Full entry → is a blueprint-like definition that creates a new kind of object. Calling the class creates an instance An object created by calling a class. Full entry →. Each instance can carry its own attributes, while a method A function defined in a class that can be accessed through an instance. Full entry → is behavior defined in the class and used through an instance. This lesson focuses on the small mechanics: defining a class, storing per-instance state with self, and recognizing the difference between a class attribute An attribute assigned in the class body and available through the class or through instances that do not shadow it. Full entry → and an instance attribute An attribute stored on one particular instance, often assigned through self. Full entry →.
Why this matters
Classes let a program keep related data and behavior together. A Student, Timer, or CartItem can each be represented by an object whose attributes describe that particular thing and whose methods operate on it. That makes repeated patterns easier to name and use without making every variable global. The distinction between shared class data and per-instance data also prevents a subtle but common bug: accidentally giving every object the same mutable list or dictionary.
The college version
A class defines a kind of object
Python programs work with objects: values that have an identity, a type, and a value. A class statement is one way to define a new user-facing type. Its indented suite runs when Python executes the class statement, producing a class object. In beginner code, the useful mental model is that the class provides a shared definition, while each call to the class creates a separate instance.
Here is a minimal class:
class Badge:
pass
first = Badge()
second = Badge()Badge names the class. first and second are two different instances made by calling it. The empty pass is only a placeholder; practical classes usually define initialization and methods. A class is not a single record of data. It describes operations and attributes that instances can use. This is narrower than a full object-oriented-design discussion: the immediate question is simply which object owns a particular value or action.
Instance attributes and initialization
An attribute A named value or behavior reached through dotted notation on an object or class. Full entry → is a value reached with dotted notation, such as book.title. Classes often create instance attributes in a method named __init__. Python calls that method as part of constructing an instance. By convention, its first parameter is named self. In a call such as Book("The Left Hand of Darkness"), Python supplies the new instance as self; the programmer supplies the title argument.
class Book:
def __init__(self, title):
self.title = title
first = Book("The Left Hand of Darkness")
second = Book("Kindred")
print(first.title)
print(second.title)This prints the two different titles. The assignment self.title = title stores an attribute on the particular object received as self. It does not create one universal title for every Book. Later, first.title = "A Wizard of Earthsea" changes only first's attribute. That per-instance separation is why objects are useful for representing several similar things with different state.
Methods, self, and attribute lookup
A method is a function defined in a class body. Accessing it through an instance creates the normal method-call connection: first.describe() uses first as the first argument. self is not a special keyword; it is the conventional parameter name that makes this relationship readable. Inside a method, self.title means “the title on the instance that received this call.”
class Book:
def __init__(self, title):
self.title = title
def describe(self):
return f"Reading: {self.title}"
first = Book("Kindred")
print(first.describe())The result is Reading: Kindred. Defining describe in the class gives each Book instance the same behavior, but the behavior reads each instance's own data. Do not write Book.describe() as a substitute for first.describe() here: the method needs an instance for its self parameter. This lesson's nearby functions and return-values topics own general function syntax and return mechanics; here they are used only to show a method attached to a class.
Class attributes are shared defaults
A name assigned directly in the class suite is a class attribute. Instances can read it when they do not have an instance attribute with the same name. That makes class attributes suitable for a shared constant or default. An instance assignment with the same name creates an instance attribute that shadows the class attribute for that one object.
class Meter:
unit = "km"
def __init__(self, value):
self.value = value
def label(self):
return f"{self.value} {self.unit}"
a = Meter(3)
b = Meter(7)
Meter.unit = "mi"
a.unit = "m"
print(a.label())
print(b.label())The output is 3 m and 7 mi. Changing Meter.unit changes the class value that b finds. Assigning a.unit gives a its own value, so it no longer uses the class value. A critical caution follows: do not use a mutable object such as [] as a class attribute when each instance needs a separate collection. Put self.items = [] in __init__ instead; otherwise instances can observe and modify the same list.

Eli explains
The same idea, in plain words
Explain it like I’m 10
Imagine a class as instructions for making labeled storage boxes. The instructions say each box needs a label and can describe what it holds. When you make two boxes, they follow the same instructions but have different labels. In Python, the class is the instructions and each box is an instance. self means “this particular box,” so self.label changes or reads the label on the box currently being used. A method is an action the instructions give every box, such as showing its label.
Picture it like this
A cookie cutter is like a class: one cutter can make many cookies with the same shape. Each cookie is an instance, and icing written on one cookie is like an instance attribute because it belongs to that cookie. A note printed on the cutter is like a class attribute: every cookie can use that shared default until one cookie gets its own version.
Where the picture stops working
Cookies do not run actions or look up attributes, and a real cutter does not literally store data for cookies. Python's class attributes are names Python searches for, not physical writing copied into each instance. The analogy is only for separating a shared definition from individual objects.
Worked example
Suppose a library app needs two books. Define Book with __init__, setting self.title from the supplied title, and define describe to return a sentence using that attribute. Construct first = Book("Kindred") and second = Book("The Left Hand of Darkness"). Calling first.describe() passes first to self, so it returns Reading: Kindred; calling second.describe() returns the second title. The method code is shared, but its result differs because each instance owns a different title. If the program assigns first.title = "A Wizard of Earthsea", only the first result changes.
Key takeaway
Use a class to define related state and behavior, then create instances for individual things. Read and write instance state through self, and reserve class attributes for values intentionally shared across instances.
Quick check
3 questions here, of 5 in this lesson’s practice set. Answers stay hidden until you check.
What does
In def describe(self): return self.title, what does self refer to when first.describe() runs?
Two Book instances have titles assigned with self.title in __init__. After first.title = "New", what happens to second.title?
Study tools & related lessonsYou’ll learn to · Common mistakes · Easily confused · Key vocabulary · Related
You’ll learn to
- Define a class and distinguish it from an instance.
- Create instances by calling a class.
- Explain how
selfconnects an instance method to the instance receiving the call. - Apply dotted attribute access to read and change per-instance state.
- Distinguish a class attribute from an instance attribute and predict basic lookup behavior.
Common mistakes
Treating the class name as though it were already a particular object.
Call the class to create an instance when code needs per-object state.
Writing an instance method without a first parameter.
Use the conventional
selfparameter when the method needs the receiving instance.Expecting
self.nameto be shared by every instance.Assignments through
selfcreate or change attributes on that one instance.Putting a per-instance mutable list in the class body.
Create a new list in
__init__, for exampleself.items = [].
Easily confused
class vs. instance
A class is the shared definition; an instance is one object created from it.
class attribute vs. instance attribute
A class attribute is a shared lookup value; an instance attribute belongs to one object and can shadow a class attribute.
Key vocabulary
- class
- An object created by a class statement that defines attributes and behavior for instances.
- instance
- An object created by calling a class.
- attribute
- A named value or behavior reached through dotted notation on an object or class.
- instance attribute
- An attribute stored on one particular instance, often assigned through self.
- class attribute
- An attribute assigned in the class body and available through the class or through instances that do not shadow it.
- method
- A function defined in a class that can be accessed through an instance.
- self
- The conventional first parameter of an instance method; it refers to the instance receiving the call.
Sources & references
- The Python Tutorial — 9.2 Python Scopes and Namespaces — Python Software Foundation
- 8. Compound statements - The Python Language Reference (while, for, break, continue) — Python Software Foundation
- The Python Language Reference — Data model — Python Software Foundation
EliExplains lessons are original prose written from the open, credible references above. See Copyright & Licensing.
Researched 2026-08-19
Educational content only. It is not medical, legal or professional advice. Found an error? Tell us.

