Skip to content

Creating Classes and Objects in Python

let us see how to define classes, create objects (instances), and work with their attributes in Python. This article assumes we're already familiar with the concepts of classes and objects.


1. Defining a Class and Creating Objects

A class defines a new data type, while an object (or instance) is an individual value created from that data type.

A class is defined using the class keyword.

class Employee:
    pass

The pass statement indicates that the class currently contains no attributes or methods.

Once a class has been defined, objects can be created by calling the class name like a function.

emp1 = Employee()
emp2 = Employee()

Here:

  • Employee is the class.
  • emp1 and emp2 are two different objects (instances) of the Employee class.

Each call to Employee() creates a new object.

print(type(emp1))
print(type(emp2))

Output

<class '__main__.Employee'>
<class '__main__.Employee'>

Although emp1 and emp2 belong to the same class, they are independent objects.


2. Defining Attributes Within a Class

An attribute is a piece of data that belongs to an object.

For example, an Employee object might store attributes such as:

  • name
  • age
  • department

One way to define these attributes is to declare them directly within the class.

class Employee:
    name = ""
    age = 0
    department = ""

emp1 = Employee()
emp2 = Employee()

Here, the class defines three attributes:

  • name
  • age
  • department

Every object created from the Employee class has access to these attributes.

print(emp1.name)
print(emp1.age)
print(emp1.department)

Output

0

Each object can assign its own values to these attributes.

emp1.name = "Sergei"
emp1.age = 25
emp1.department = "Engineering"

emp2.name = "Boris"
emp2.age = 30
emp2.department = "Sales"

print(emp1.name, emp1.age, emp1.department)
print(emp2.name, emp2.age, emp2.department)

Output

Sergei 25 Engineering
Boris 30 Sales

This approach is similar to languages such as Java and C++, where the expected data members are declared when the class is defined.


3. Creating Attributes Dynamically

Unlike many programming languages, Python also allows attributes to be created dynamically.

Instead of defining attributes within the class, they can be created simply by assigning values to an object.

class Employee:
    pass

emp = Employee()

emp.name = "Sergei"
emp.age = 25
emp.department = "Engineering"

Each assignment creates a new attribute for the object.

The same dot (.) operator is used to access the attribute values.

print(emp.name)
print(emp.age)
print(emp.department)

Output

Sergei
25
Engineering

Since attributes are created dynamically, different objects can contain different sets of attributes.

class Employee:
    pass

emp1 = Employee()
emp2 = Employee()

emp1.name = "Sergei"
emp1.age = 25

emp2.name = "Boris"
emp2.salary = 75000

Here:

  • emp1 has the attributes name and age.
  • emp2 has the attributes name and salary.

Trying to access an attribute that doesn't exist raises an AttributeError.

print(emp1.salary)

Output

AttributeError: 'Employee' object has no attribute 'salary'

Although Python supports dynamic attributes, most Python programs initialize an object's attributes using a constructor.


4. Initializing Attributes Using the Constructor

Python provides a special method named __init__() to initialize an object's attributes.

This method is called the constructor.

class Employee:

    def __init__(self):
        self.name = ""
        self.age = 0
        self.department = ""

Whenever an object is created, Python automatically calls the constructor.

emp1 = Employee()
emp2 = Employee()

Each object begins with its own name, age, and department attributes.

emp1.name = "Sergei"
emp1.age = 25
emp1.department = "Engineering"

emp2.name = "Boris"
emp2.age = 30
emp2.department = "Sales"

print(emp1.name, emp1.age, emp1.department)
print(emp2.name, emp2.age, emp2.department)

Output

Sergei 25 Engineering
Boris 30 Sales

The constructor can also accept arguments so that each object is initialized with different values.

class Employee:

    def __init__(self, name, age, department):
        self.name = name
        self.age = age
        self.department = department

emp1 = Employee("Sergei", 25, "Engineering")
emp2 = Employee("Boris", 30, "Sales")


5. Complete Example

The following Employee class combines everything we've learned so far.

Employee
├── Attributes
│   ├── name
│   ├── age
│   └── department
└── Constructor
    └── __init__(name, age, department)

The class defines three attributes and initializes them using the __init__() constructor.

class Employee:

    def __init__(self, name, age, department):
        self.name = name
        self.age = age
        self.department = department


emp1 = Employee("Sergei", 25, "Engineering")
emp2 = Employee("Boris", 30, "Sales")

print(emp1.name, emp1.age, emp1.department)
print(emp2.name, emp2.age, emp2.department)

Output

Sergei 25 Engineering
Boris 30 Sales

In this example:

  • Employee defines a new data type.
  • emp1 and emp2 are objects of the Employee class.
  • The __init__() constructor initializes each object with its own data.
  • The attributes are accessed using the dot (.) operator.

6. Understanding the self Parameter

The first parameter of every instance method, including the constructor, is usually named self.

class Employee:

    def __init__(self, name, age):
        self.name = name
        self.age = age

The self parameter refers to the object that is currently being created or accessed.

When we create an object,

emp = Employee("Sergei", 25)

Python automatically passes the newly created object as the first argument to the constructor.

Although self appears in the method definition, we never pass it explicitly.

emp = Employee("Sergei", 25)      # Correct

Not

emp = Employee(emp, "Sergei", 25) # Incorrect

The self parameter is used to create and access attributes that belong to the current object.

print(emp.name)
print(emp.age)

Output

Sergei
25

Summary

In this article, we learned how to define a class, create objects, define attributes within a class, create attributes dynamically, initialize objects using the __init__() constructor, and understand the purpose of the self parameter.

These concepts provide the foundation for working with classes and objects in Python.


Python Material on this website