Sitemap

OOPS IN PYTHON

6 min readJan 5, 2023

--

OOP stands for Object-Oriented Programming. It is a programming paradigm based on the concept of “objects”, which can contain data and code that manipulates that data. In Python, OOP concepts are implemented using classes and objects.

Here is a simple example of a Python class:

class Dog:
def __init__(self, name, breed):
self.name = name
self.breed = breed

def bark(self):
print("Woof!")

dog1 = Dog("Fido", "Labrador")
dog2 = Dog("Buddy", "Poodle")

print(dog1.name) # Output: "Fido"
print(dog2.breed) # Output: "Poodle"
dog1.bark() # Output: "Woof!"

In this example, the Dog class is defined with a __init__ method that is called when a new Dog object is created. The __init__ method initializes the name and breed attributes of the object. The class also has a method called bark, which prints "Woof!" to the console when called.

Two Dog objects are created and their attributes are accessed using dot notation (e.g. dog1.name). The bark method is also called on the dog1 object.

Class

In Python, a class is a template for creating objects. It is a blueprint for an object and defines the attributes and behaviors of an object.

Here is an example of a simple class in Python:

class Dog:
def __init__(self, name, breed):
self.name = name
self.breed = breed

def bark(self):
print("Woof!")

dog1 = Dog("Fido", "Labrador")
dog2 = Dog("Buddy", "Poodle")

In this example, the Dog class has two attributes: name and breed. It also has a method called bark, which prints "Woof!" to the console when called.

To create an object from a class, you use the class name followed by parentheses. In this example, dog1 and dog2 are objects of the Dog class. You can access the attributes of an object using dot notation (e.g. dog1.name) and you can call an object's methods using dot notation and parentheses (e.g. dog1.bark()).

Object

An object is an instance of a class. When you define a class, you define the general behavior that a whole category of objects can have. When you create an object from a class, you create a specific instance of that class.

For example, consider the following Dog class:

class Dog:
def __init__(self, name, breed):
self.name = name
self.breed = breed

def bark(self):
print("Woof!")

To create a specific dog object from this class, you would write:

dog1 = Dog("Fido", "Labrador")

This creates a Dog object with the name "Fido" and breed "Labrador". You can then access the attributes of this object using dot notation (e.g. dog1.name) and you can call its methods using dot notation and parentheses (e.g. dog1.bark()).

You can create multiple objects from a single class. For example:

dog2 = Dog("Buddy", "Poodle")
dog3 = Dog("Charlie", "Beagle")

Each of these objects would have its own set of attributes and behaviors, but they would all be instances of the Dog class.

Get Akheelmohammad’s stories in your inbox

Join Medium for free to get updates from this writer.

An object consists of :

  • State: It is represented by the attributes of an object. It also reflects the properties of an object.
  • Behavior: It is represented by the methods of an object. It also reflects the response of an object to other objects.
  • Identity: It gives a unique name to an object and enables one object to interact with other objects.

To understand the state, behavior, and identity let us take the example of the class dog (explained above).

  • The identity can be considered as the name of the dog.
  • State or Attributes can be considered as the breed, age, or color of the dog.
  • The behavior can be considered as to whether the dog is eating or sleeping.

Class Attributes and Methods

When we design a class, we use instance variables and class variables.

In Class, attributes can be defined into two parts:

  • Instance variables: The instance variables are attributes attached to an instance of a class. We define instance variables in the constructor ( the __init__() method of a class).
  • Class Variables: A class variable is a variable that is declared inside of class, but outside of any instance method or __init()__ method.

Inside a Class, we can define the following three types of methods.

  • Instance method: Used to access or modify the object attributes. If we use instance variables inside a method, such methods are called instance methods.
  • Class method: Used to access or modify the class state. In method implementation, if we use only class variables, then such type of methods we should declare as a class method.
  • Static method: It is a general utility method that performs a task in isolation. Inside this method, we don’t use instance or class variable because this static method doesn’t have access to the class attributes.

Example:

# class methods demo
class Student:
# class variable
school_name = 'ABC School'

# constructor
def __init__(self, name, age):
# instance variables
self.name = name
self.age = age

# instance method
def show(self):
# access instance variables and class variables
print('Student:', self.name, self.age, Student.school_name)

# instance method
def change_age(self, new_age):
# modify instance variable
self.age = new_age

# class method
@classmethod
def modify_school_name(cls, new_name):
# modify class variable
cls.school_name = new_name

s1 = Student("Harry", 12)

# call instance methods
s1.show()
s1.change_age(14)

# call class method
Student.modify_school_name('XYZ School')
# call instance methods
s1.show()

Output:

Student: Harry 12 ABC School
Student: Harry 14 XYZ School

Representation

Constructors in Python

In Python, a constructor is a special type of method used to initialize the object of a Class. The constructor will be executed automatically when the object is created. If we create three objects, the constructor is called three times and initialize each object.

The main purpose of the constructor is to declare and initialize instance variables. It can take at least one argument that is self. The __init()__ method is called the constructor in Python. In other words, the name of the constructor should be __init__(self).

The four pillars of object-oriented programming (OOP) are:

  1. Encapsulation: Encapsulation refers to the bundling of data with the methods that operate on that data. In OOP, encapsulation is achieved by creating classes, which define the attributes and behaviors of an object, and then creating objects from those classes.
  2. Abstraction: Abstraction is the process of exposing only the essential features of an object and hiding the implementation details. In OOP, abstraction is achieved through the use of abstract classes and methods, which define the interface of an object without providing an implementation.
  3. Inheritance: Inheritance is a programming principle that allows a class to inherit attributes and behaviors from a parent class. In OOP, inheritance is used to create a hierarchy of classes, where a derived class can inherit and override the attributes and behaviors of a base class.
  4. Polymorphism: Polymorphism is the ability of a single entity (such as a function or method) to take on multiple forms. In OOP, polymorphism is often achieved through inheritance, where a subclass can override or extend the methods of its parent class. Polymorphism is also achieved through function overloading and the use of interfaces.

--

--