Sitemap
Bhavatharani

Bhavatharani

OOPS CONCEPT IN PYTHON

--

Python is a multi-paradigm programming language. It supports different programming approaches.

One of the popular approaches to solve a programming problem is by creating objects. This is known as Object-Oriented Programming (OOP).

An object has two characteristics:

  • attributes
  • behavior

Let’s take an example:

A parrot is an object, as it has the following properties:

  • name, age, color as attributes
  • singing, dancing as behavior

The concept of OOP in Python focuses on creating reusable code. This concept is also known as DRY (Don’t Repeat Yourself).

Press enter or click to view image in full size

The main concepts of OOPs are:

  1. Class
  2. Objects
  3. Polymorphism
  4. Encapsulation
  5. Inheritance

Class:

A class is a blueprint for the object.

Get Bhavatharani’s stories in your inbox

Join Medium for free to get updates from this writer.

We can think of class as a sketch of a parrot with labels. It contains all the details about the name, colors, size etc. Based on these descriptions, we can study about the parrot. Here, a parrot is an object.

Object:

An object (instance) is an instantiation of a class. When class is defined, only the description for the object is defined. Therefore, no memory or storage is allocated.

Inheritance:

Inheritance is a way of creating a new class for using details of an existing class without modifying it. The newly formed class is a derived class (or child class). Similarly, the existing class is a base class (or parent class).

Encapsulation

Using OOP in Python, we can restrict access to methods and variables. This prevents data from direct modification which is called encapsulation. In Python, we denote private attributes using underscore as the prefix i.e single -_or double __.

Polymorphism

Polymorphism is an ability (in OOP) to use a common interface for multiple forms (data types).

Suppose, we need to color a shape, there are multiple shape options (rectangle, square, circle). However we could use the same method to color any shape. This concept is called Polymorphism.

--

--