← Back to Chapters

Python Classes and Objects

? Python Classes and Objects

⚡ Quick Overview

A class is a blueprint for creating objects. It defines the attributes (data) and methods (functions) that its objects will have. Objects are instances of a class and can hold their own unique data while sharing the behavior defined by the class.

? Key Concepts

  • Class: A blueprint or template that describes what data and behavior its objects will have.
  • Object: A concrete instance created from a class; it has real data stored in its attributes.
  • __init__(): A special initializer method that runs automatically when an object is created.
  • self: Refers to the current object instance; used to access attributes and methods.
  • Attributes: Variables that belong to an object (e.g., name, breed).
  • Methods: Functions defined inside a class that describe the behavior of its objects (e.g., bark()).

? Syntax and Theory

A basic Python class is defined using the class keyword. The __init__() method initializes the attributes for each new object created from the class.

When you create an object (also called instantiating a class), Python calls __init__() automatically and passes the arguments you provide to it.

? Creating a Class and an Object

Here is a simple Dog class with two attributes (name and breed) and one method (bark()).

? View Code Example – Define Class and Create One Object
class Dog:
    def __init__(self, name, breed):
        self.name = name      # instance attribute
        self.breed = breed    # instance attribute

    def bark(self):
        print(f"{self.name} is barking!")

# Creating an object
dog1 = Dog("Buddy", "Golden Retriever")

? Working with Objects

Once an object is created, you can access its attributes and call its methods using the dot (.) operator.

? View Code Example – Access Attributes and Call Methods
print(dog1.name)   # Buddy
print(dog1.breed)  # Golden Retriever

dog1.bark()        # Buddy is barking!

? Multiple Objects from One Class

You can create many objects from the same class. Each object has its own data but shares the structure and behavior defined by the class.

? View Code Example – Multiple Dog Objects
dog2 = Dog("Charlie", "Beagle")
dog3 = Dog("Max", "German Shepherd")

dog2.bark()  # Charlie is barking!
dog3.bark()  # Max is barking!

⚙️ Class vs Object

You can think of a class as a blueprint and an object as the actual thing built from it:

  • A class is like a recipe for a cake.
  • An object is the actual cake you bake using that recipe.
  • You can use the same recipe (class) to bake many cakes (objects), each with its own decorations (data).

? Live Output

? Expected Output

If you run all of the code examples together, you would see something like:

# Expected output when running all the code above
Buddy
Golden Retriever
Buddy is barking!
Charlie is barking!
Max is barking!

Each call to bark() prints a message that includes the name of the specific dog object. Even though all objects are created from the same Dog class, they carry their own values for name and breed.

? Tips and Best Practices

  • Think of a class as a recipe and objects as cakes made from that recipe.
  • You can create as many objects from a class as you need; each object can hold its own data.
  • Always include self as the first parameter in methods defined inside a class.
  • Use methods to define what your objects can do (their behavior).
  • Remember: instance attributes (like self.name) belong to each object separately.

? Try It Yourself

  • Create a Car class with attributes brand and year. Then make three different car objects.
  • Write a Student class with a method study() and call it using two different student objects.
  • Build an Animal class and create different animal objects with their names.
  • Experiment by adding more methods (like start(), stop(), eat()) to your classes.