Python Classes and Objects: OOP Fundamentals for Beginners

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Object-Oriented Programming in Python

Object-oriented programming (OOP) is one of the most important paradigms in modern software development. Python fully supports OOP, making it a powerful tool for both beginner developers and professional programmers.

OOP helps model the real world, improve code readability, reuse components, and manage project complexity. The key concepts of object-oriented programming are classes and objects, along with the core principles: inheritance, encapsulation, and polymorphism.

This approach structures a program as a collection of interacting objects, each of which is an instance of a specific class. This makes the code more modular, understandable, and easier to maintain.

Python Classes and Objects Basics

Defining a Class and Creating an Object

A class is a blueprint or template for creating objects. It defines the data structure and methods that will be available to all objects of that type.

An object is a specific instance of a class that holds its own data and can perform actions defined in the class.

class Car:
    def drive(self):
        print("The car is moving")

my_car = Car()  # object (class instance)
my_car.drive()

In the code above, the Car class defines a blueprint for creating car objects. The drive() method describes behavior available to all instances of this class.

Class Structure in Python

The basic structure of a class in Python looks like this:

class ClassName:
    def __init__(self, arguments):
        self.properties = values
    
    def method(self):
        actions

Key components of a class include:

  • init — the class constructor, automatically called when a new object is created
  • self — a special parameter representing a reference to the current object instance
  • Attributes — variables that store object data
  • Methods — functions defined inside a class and tied to the object

Creating and Using Classes

Practical Example of Creating a Class

Let's look at creating a Person class that models a person:

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    
    def greet(self):
        print(f"Hi, my name is {self.name}")
    
    def get_info(self):
        return f"Name: {self.name}, Age: {self.age}"

p = Person("Anna", 30)
p.greet()
print(p.get_info())

In this example, the init constructor takes name and age parameters, which are saved as instance attributes. The greet() and get_info() methods provide functionality for working with the object's data.

Creating Class Instances

The process of creating a class instance in Python happens by calling the class like a function:

p1 = Person("John", 25)
p2 = Person("Maria", 28)

print(p1.name)  # John
print(p1.age)   # 25
print(p2.name)  # Maria

Each call to the class constructor creates a new independent object with its own attributes and state.

Attributes in Python Classes

Instance Attributes

Instance attributes are unique to each object. They are created and initialized in the init method and can be modified during program execution:

class Student:
    def __init__(self, name, grade):
        self.name = name
        self.grade = grade
    
    def update_grade(self, new_grade):                

Blogs

Book Recommendations