12 OOPS in Python

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Introduction to OOPs in Python

Python is a multi-paradigm programming language. Meaning, it supports different


programming approach.

One of the popular approach 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:

Parrot is an object,

 name, age, color are attributes


 singing, dancing are behavior

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

In Python, the concept of OOP follows some basic principles:

A process of using details from a new class without modifying


Inheritance
existing class.

Encapsulation Hiding the private details of a class from other objects.

A concept of using common operation in different ways for


Polymorphism
different data input.

Class
A class is a blueprint for the object.

We can think of class as an 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, parrot is an object.

The example for class of parrot can be :


class Parrot:

pass

Here, we use class keyword to define an empty class Parrot. From class, we
construct instances. An instance is a specific object created from a particular class.

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.

The example for object of parrot class can be:

obj = Parrot()

Here, obj is object of class Parrot.

Suppose we have details of parrot. Now, we are going to show how to build the class
and objects of parrot.

Example 1: Creating Class and Object in Python


class Parrot:
# class attribute
species = "bird"
# instance attribute
def __init__(self, name, age):
self.name = name
self.age = age
# instantiate the Parrot class
blu = Parrot("Blu", 10)
woo = Parrot("Woo", 15)
# access the class attributes
print("Blu is a {}".format(blu.__class__.species))
print("Woo is also a {}".format(woo.__class__.species))
# access the instance attributes
print("{} is {} years old".format( blu.name, blu.age))
print("{} is {} years old".format( woo.name, woo.age))

When we run the program, the output will be:


Blu is a bird

Woo is also a bird

Blu is 10 years old

Woo is 15 years old

In the above program, we create a class with name Parrot. Then, we define
attributes. The attributes are a characteristic of an object.

Then, we create instances of the Parrot class. Here, blu and woo are references
(value) to our new objects.

Then, we access the class attribute using __class __.species. Class attributes are
same for all instances of a class. Similarly, we access the instance attributes
using blu.name and blu.age. However, instance attributes are different for every
instance of a class.

Methods
Methods are functions defined inside the body of a class. They are used to define
the behaviors of an object.

Example 2 : Creating Methods in Python


class Parrot:

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

# instance method
def sing(self, song):
return "{} sings {}".format(self.name, song)
def dance(self):
return
"{} is now dancing".format(self.name)
# instantiate the object
blu = Parrot("Blu", 10)
# call our instance methods
print(blu.sing("'Happy'"))
print(blu.dance())
When we run program, the output will be:

Blu sings 'Happy'

Blu is now dancing

In the above program, we define two methods i.e sing() and dance(). These are
called instance method because they are called on an instance object i.e blu.

Inheritance
Inheritance is a way of creating new class for using details of 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).

Example 3: Use of Inheritance in Python


# parent class
class Bird:

def __init__(self):
print("Bird is ready")
def whoisThis(self):
print("Bird")
def swim(self):
print("Swim faster")
# child class
class Penguin(Bird):
def __init__(self):
# call super() function
super().__init__()
print("Penguin is ready")
def whoisThis(self):
print("Penguin")
def run(self):
print("Run faster")
peggy = Penguin()
peggy.whoisThis()
peggy.swim()
peggy.run()

When we run this program, the output will be:

Bird is ready
Penguin is ready

Penguin

Swim faster

Run faster

In the above program, we created two classes i.e. Bird (parent class)
and Penguin (child class). The child class inherits the functions of parent class. We
can see this from swim()method. Again, the child class modified the behavior of
parent class. We can see this from whoisThis() method. Furthermore, we extend the
functions of parent class, by creating a new run() method.

Additionally, we use super() function before __init__() method. This is because we


want to pull the content of __init__() method from the parent class into the child
class.

You might also like