Logo
READLEARNKNOWCONNECT
Back to Lessons

    Page

  • - What Are Classes and Objects?
  • - Defining a Class
  • - The __init__ Constructor
  • - Instance Attributes
  • - Class Attributes
  • - Instance Methods
  • - Class Methods and Static Methods
  • - Mini Project Step

14. Classes and Objects in Python

Level: IntermediateDuration: 35m

What Are Classes and Objects?

In Python, everything is an object — strings, lists, functions, even classes themselves. A class is a code template used to create objects, and objects are independent containers of data and behavior. Classes help organize code into logical, reusable pieces, especially in large projects.

If you think in real-world terms, a class is like a category, while objects are individual members of that category. For example, 'Car' can be a class, and 'Honda Civic 2018' or 'Tesla Model Y' are objects created from that class.

Defining a Class

You define a class using the `class` keyword. At first, a class can be empty and still valid.

python
class Car:
    pass

my_car = Car()
print(type(my_car))

`my_car` is now an object (an instance) of the `Car` class.

The __init__ Constructor

When you create an object from a class, Python automatically calls a special method named `__init__`. This method is known as a constructor and is often used to initialize object attributes.

python
class Car:
    def __init__(self, brand, year):
        self.brand = brand
        self.year = year

car1 = Car("Toyota", 2020)
print(car1.brand, car1.year)
💡 `self` always refers to the current object instance. Without it, Python won't know which object's data you want to access.

Instance Attributes

Instance attributes belong to a specific object. Each object can store its own data independently of other objects, even if they're created from the same class.

Class Attributes

Unlike instance attributes, class attributes are shared across all objects of a class. They're useful for data that should be consistent for every instance.

python
class Car:
    wheels = 4  # class attribute shared by all cars
    def __init__(self, brand):
        self.brand = brand  # instance attribute

car1 = Car("Honda")
car2 = Car("BMW")
print(car1.wheels, car2.wheels)

Changing a class attribute affects all objects, unless the attribute is overridden inside an object.

Instance Methods

Methods are functions defined inside a class. Instance methods operate on object data and must include `self` as their first parameter.

python
class Car:
    def __init__(self, brand):
        self.brand = brand

    def drive(self):
        return f"{self.brand} is driving."

car = Car("Tesla")
print(car.drive())

Class Methods and Static Methods

Class methods work with the class itself rather than individual objects. Static methods are utility methods that logically belong to a class but don’t access class or instance data.

python
class MathUtils:
    @staticmethod
    def add(a, b):
        return a + b

print(MathUtils.add(3, 5))

Static methods help organize code logically inside classes instead of floating around as standalone functions.

Deep Dive: Classes and Objects with Real Examples

Mini Project Step

Enhance your `Calculator` class by adding a class attribute `operations_count` and increment it inside each method to track total operations performed. Also create a static method called `info()` that prints a short message about the calculator.