In the previous sections, we discussed the idea of object-oriented programming, where we think about organizing our code around 'objects' – bundles of data and the functions that operate on that data. The first step in creating these objects is to define a template or blueprint that specifies what kind of data they will hold and what actions they can perform. In Python, this blueprint is called a class.
Think of a class like an architect's blueprint for a house. The blueprint itself isn't a house, but it contains all the specifications needed to build one (or many identical houses). Similarly, a class definition doesn't create an object directly, but it defines the structure and behavior for all objects created from it.
To define a class in Python, we use the class
keyword, followed by the name we want to give our class, and then a colon (:
). The code that makes up the class definition is indented underneath the class
line, just like with functions or control flow statements.
The convention in Python is to name classes using CamelCase
, where each word starts with a capital letter and there are no underscores (e.g., MyClass
, Dog
, NetworkConnection
).
Here is the most basic structure of a class definition:
class ClassName:
pass
Let's break this down:
class
: This keyword signals the start of a class definition.ClassName
: Replace this with the actual name you choose for your class (following the CamelCase convention is recommended).:
: The colon indicates the start of the indented block containing the class body.pass
: This is a Python statement that does absolutely nothing. It acts as a placeholder. Since Python requires an indented block after the colon in a class definition (just like with if
, for
, def
, etc.), we use pass
when we want to define the class structure but haven't added any specific attributes or methods yet. It satisfies the syntax requirement for a non-empty block.For example, if we wanted to create a blueprint for representing dogs in our program, we could start with a simple class definition like this:
class Dog:
# This is the blueprint for creating Dog objects.
# For now, it's empty, but we'll add details later.
pass
Executing this code doesn't print anything or create any actual Dog
objects. What it does is create a new type named Dog
. We have defined the blueprint.
Inside the class definition (where pass
is currently), we will later add:
These attributes and methods form the core of the class definition, fleshing out the blueprint. For now, understanding the basic class ClassName:
syntax and the role of pass
as a placeholder is the essential first step.
Class definitions are typically placed at the top level of a Python file (module), making them available for use throughout that file and potentially importable into others.
Now that we understand how to lay out the blueprint using the class
keyword, the next step is to see how we can actually build something from that blueprint – that is, how to create individual objects (instances) from our class definition.
© 2025 ApX Machine Learning