Object-oriented programming (OOP) is a programming paradigm that provides a way to structure and organize code by creating objects that contain both data and functions. Python, a powerful and versatile programming language, fully supports object-oriented programming.
What is an Object?
In OOP, an object is an instance of a class. A class serves as a blueprint for creating objects. It defines the properties (attributes) and behaviors (methods) that objects of that class will have.
Consider a class called “Car.” Each car has attributes such as color, brand, and model.
Additionally, cars can perform actions like starting the engine or accelerating. When we create an object from this class, we instantiate it with specific values for its attributes. Now we have a car object with its own unique set of attribute values.
Defining Classes in Python
To define a class in Python, use the
class keyword followed by the name of the class. Class names are conventionally written in CamelCase format.
Within a class, we can define attributes using variables and behaviors using methods.
Attributes represent data associated with an object. They can be any valid Python data type such as strings, numbers, or even other objects.
To define attributes within a class, assign values to them within the special method called
__init__(). This method is known as the constructor and is executed when creating new instances (objects) of the class.
def __init__(self, color, brand, model):
In this example constructor method,
self refers to the instance being created. We assign the passed values (color, brand, and model) to the corresponding attributes of the instance.
Methods define the actions that an object can perform. They are similar to functions but are defined within a class and can access the object’s attributes.
start_engine() method is defined within the class. It takes
self as a parameter (which refers to the instance calling the method) and contains code to start the car’s engine.
To create an object from a class, call the class name followed by parentheses, passing any required arguments to the constructor.
my_car = Car("Red", "Toyota", "Camry")
This line creates a new instance of the
Car class with attribute values “Red,” “Toyota,” and “Camry.”
Accessing Attributes and Invoking Methods
To access an attribute or invoke a method of an object, use dot notation.
This line prints the value of the color attribute of
my_car, which is “Red.”
Inheritance in OOP
One of OOP’s powerful features is inheritance. It allows classes to inherit attributes and behaviors from other classes, establishing a hierarchy. The derived class (child) inherits properties from its base class (parent).
Python supports single inheritance, meaning a derived class can only inherit from one base class. However, multiple inheritance can be achieved using mixins or interfaces.
To create a derived (child) class, define it similar to any other class but specify its base class in parentheses after the class name.
In this example,
ElectricCar is a derived class that inherits from the
Car base class. It will have all the attributes and methods of a regular car, plus any additional ones defined specifically for electric cars.
Object-oriented programming is a powerful paradigm that helps in organizing and structuring code. Python’s support for OOP allows developers to create reusable and modular code by defining classes, objects, attributes, and methods. Inheritance further enhances code reusability and promotes code extensibility.
By understanding OOP concepts in Python, you can create efficient, scalable, and well-organized programs.