Object Oriented Programming (OOP) is a programming paradigm that is based on the concept of objects, which can contain data and functions that operate on that data. OOP provides a way to structure code in a logical and organized manner, making it easier to maintain, extend and reuse. Python is an object-oriented programming language, and its core data structures, such as lists, dictionaries, and strings, are objects. In this article, we will take a closer look at the basics of OOP in Python.
Classes and Objects
In OOP, a class is a blueprint or a template for creating objects. An object is an instance of a class and is created from the class definition. Each object has its own unique set of attributes (data) and behaviors (functions). For example, consider the following code that defines a class called “Person”:
class Person: def __init__(self, name, age): self.name = name self.age = age def say_hello(self): print("Hello, my name is", self.name)
In the code above, the class “Person” has two attributes, “name” and “age”, and one method, “say_hello”. The method “init” is a special method that is called when an object is created from the class. It is used to initialize the attributes of the object. The “self” parameter refers to the object being created and is used to access the attributes and methods of the object.
To create an object from the class “Person”, we simply call the class name as if it were a function:
person = Person("John Doe", 30) person.say_hello() # Output: Hello, my name is John Doe
Inheritance is a mechanism that allows a new class to inherit attributes and behaviors from an existing class. The new class is called a subclass, and the existing class is called the superclass. Inheritance is used to create a hierarchy of classes and to avoid repeating code. For example, consider the following code that defines a subclass “Student” that inherits from the superclass “Person”:
class Student(Person): def __init__(self, name, age, student_id): Person.__init__(self, name, age) self.student_id = student_id def say_hello(self): print("Hello, my name is", self.name, "and I am a student with id", self.student_id)
In the code above, the class “Student” inherits the attributes “name” and “age” from the superclass “Person”. The “Student” class also has its own attributes and methods. The method “say_hello” is overridden to provide a new implementation that includes the student’s ID. When we create an object from the “Student” class, it will have all the attributes and behaviors of the “Person” class as well as the attributes and behaviors of the “Student” class:
student = Student("Jane Doe", 20, "123456") student.say_hello() # Output: Hello, my name is Jane Doe and I am a student with id 123456
Polymorphism is a concept in OOP that allows objects of different classes to be treated as objects of a common class. This means that the same method name can be used for objects of different classes, and the appropriate implementation will be called based on the object’s class. Polymorphism is achieved through method overriding and method overloading. Method overriding is when a subclass provides its own implementation of a method that is inherited from the superclass. Method overloading is when multiple methods have the same name but with different parameters. In Python, method overloading is not explicitly supported, but polymorphism can still be achieved by using default arguments or the *args and **kwargs syntax in the method definition.
For example, consider the following code that defines a class “Animal” and two subclasses “Dog” and “Cat”:
class Animal: def make_sound(self): pass class Dog(Animal): def make_sound(self): print("Bark") class Cat(Animal): def make_sound(self): print("Meow")
In the code above, the class “Animal” has a method “make_sound”, but it does not have an implementation. The subclasses “Dog” and “Cat” override the “make_sound” method to provide their own implementation. Now, when we create objects from the “Dog” and “Cat” classes and call the “make_sound” method, the appropriate implementation will be called based on the object’s class:
dog = Dog() dog.make_sound() # Output: Bark cat = Cat() cat.make_sound() # Output: Meow
In this way, polymorphism allows us to write code that is more flexible and adaptable to changing requirements, making it easier to maintain and extend in the future.
In this article, we have introduced the basics of Object Oriented Programming (OOP) in Python. We have covered the concepts of classes and objects, inheritance, and polymorphism, and how they are used to structure code in a logical and organized manner. OOP is a powerful programming paradigm that provides a way to structure code in a way that makes it easier to understand, maintain, and extend. By learning the basics of OOP, you will be able to write more effective and efficient code in Python.