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.
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.
Here:
Employeeis the class.emp1andemp2are two different objects (instances) of theEmployeeclass.
Each call to Employee() creates a new object.
Output
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:
nameagedepartment
One way to define these attributes is to declare them directly within the class.
Here, the class defines three attributes:
nameagedepartment
Every object created from the Employee class has access to these attributes.
Output
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
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.
Output
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:
emp1has the attributesnameandage.emp2has the attributesnameandsalary.
Trying to access an attribute that doesn't exist raises an AttributeError.
Output
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.
Whenever an object is created, Python automatically calls the constructor.
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
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
In this example:
Employeedefines a new data type.emp1andemp2are objects of theEmployeeclass.- 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.
The self parameter refers to the object that is currently being created or accessed.
When we create an object,
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.
Not
The self parameter is used to create and access attributes that belong to the current object.
Output
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.