Classes and Objects

When developing complex applications, managing data and the operations that act on it can become challenging. Object-Oriented Programming (OOP) offers a structured approach to this problem by organizing code around objects, which are instances of classes. This paradigm allows us to model real-world entities and their interactions, leading to more modular, reusable, and maintainable codebases.

The Blueprint: What is a Class?

A class serves as a blueprint or a template for creating objects. It defines the common characteristics (attributes) and behaviors (methods) that all objects of a certain type will possess. Think of a class like the architectural plans for a house: it specifies the number of rooms, the layout, and the materials, but it isn't an actual house you can live in. It's a conceptual definition, not a physical entity.

Class
A blueprint or template that defines the structure (attributes) and behavior (methods) for a specific type of object.
Example: The Car class defines that all cars will have a make, model, and year, and can perform actions like start_engine().

The Instance: What is an Object?

An object is a concrete, real-world instance of a class. While the class defines the general structure, an object fills in the specific details for that structure. Continuing the house analogy, an object is an actual house built from the architectural plans, with its own unique address, paint color, and occupants. Each object has its own distinct set of data (its state) based on the class definition.

Object (Instance)
A concrete realization of a class. Each object has its own unique set of data (state) and can perform the behaviors (methods) defined by its class.
Example: A specific car, my_car, with make="Toyota", model="Camry", and year=2022 is an object (an instance) of the Car class.
Check Your Understanding
Which best describes the relationship between a class and an object?

Defining a Simple Class in Python

In Python, you define a class using the class keyword, followed by the class name (conventionally capitalized using CamelCase) and a colon. The body of the class is indented. Initially, a class might be empty, using pass as a placeholder, but it immediately allows you to create objects from it. These objects, at this stage, won't have any specific data or behavior yet.

pythonCreating a Basic Class and Object

Initializing Objects with __init__

The __init__ method is a special method in Python classes, often called the constructor. It's automatically called whenever a new object (instance) of the class is created. Its primary purpose is to initialize the object's attributes with starting values. The first parameter of __init__ (and all instance methods) is conventionally named self, which refers to the instance of the class being created or operated on.

pythonUsing `__init__` to Set Instance Attributes
The `self` Parameter

While self is the conventional name for the first parameter of instance methods, it is not a Python keyword. You could technically use another name, but it is strongly recommended to stick to self for readability and consistency within the Python community. It explicitly refers to the instance on which the method is being called.

Try It Yourself
Modify the Car class to include a color attribute. Then, create a new car object with a specified color and print its details.
python

Defining Instance Methods (Behavior)

Methods are functions defined inside a class that describe the behaviors or actions an object can perform. They operate on the object's data (its attributes). Like __init__, all instance methods must have self as their first parameter, allowing them to access and modify the specific object's attributes. This is how an object can 'do' things related to its own state.

pythonAdding Methods to a Class

Class Attributes (Shared Data)

While instance attributes are unique to each object, class attributes are shared by all instances of a class. They are defined directly within the class body, outside of any method. Class attributes are useful for storing data that is common to all objects of that type, such as a constant value or a counter for the number of objects created. Access them using ClassName.attribute_name or self.attribute_name.

pythonUsing Class Attributes for Shared Data
⚠️ Modifying Class Attributes via Instance

While you can read a class attribute via an instance (e.g., car1.wheels), modifying it using car1.wheels = 5 will not change the class attribute. Instead, it creates a new instance attribute named wheels specifically for car1, shadowing the class attribute. To truly modify the shared class attribute, always use ClassName.attribute_name = value.

Check Your Understanding
If a class attribute count is 0, and you create two objects, what is object1.count and object2.count?

The self Parameter in Detail

The self parameter is a convention that represents the instance of the class. When you call a method on an object, Python automatically passes the object itself as the first argument to the method. This allows the method to access and manipulate the object's own attributes and call other methods belonging to that specific instance. Without self, a method wouldn't know which object's data it should operate on.

pythonHow `self` Connects Methods to Instances

Encapsulation: Bundling Data and Behavior

Encapsulation is a core OOP principle where the data (attributes) and the methods that operate on that data are bundled together within a single unit—the object. This bundling helps to hide the internal state of an object from the outside world, exposing only what is necessary through well-defined interfaces (methods). It promotes data integrity and reduces complexity by preventing direct, uncontrolled access to an object's internal workings.

Class and Object Relationship
Loading diagram...
A class defines the common structure and behavior, while objects are unique instances with their own specific data.
Key Takeaways
  • A class is a blueprint defining attributes and methods, while an object is a concrete instance of that class with unique data.

  • The __init__ method acts as a constructor, automatically initializing an object's instance-specific attributes upon creation.

  • Instance attributes store data unique to each object, whereas class attributes hold data shared across all objects of a class.

  • Methods are functions within a class that define an object's behavior, operating on its instance attributes.

  • The self parameter is a convention that refers to the current object, allowing methods to access and modify its specific data.

  • Encapsulation bundles an object's data and methods, protecting internal state and simplifying interaction through defined interfaces.

← All lessons in Object-Oriented Programming

Ready to keep this from fading?

Bitelrn turns lessons like this into a full course — quizzes, a knowledge map, and spaced review.

Get started free