OOPs in Python

Nail Object Oriented Programming OOP in Python | Class and Instance | Constructor of the class | 8 mins read

Picture of topictrick
topictrick
Today we are going to talk about 'Overview of Object Oriented Programming(OOP) in Python .This tutorial covers the overview of OOPs in python , meaning of some terminologies like class,instance ,constructor .This tutorial provides the base to understand the concept of OOPs in python. Let's get started.

Table of Contents

Introduction

Welcome back to another exciting tutorial on “Object oriented Programming OOPs in Python”. In this Python tutorial, you’ll learn about the class,instance,constructor of the class etc .

You will learn :-

  • Overview Of Object Oriented Programming
  • How to create class in python
  • Instance of the class
  • Constructors of python class

If you aren’t following along, I would recommend you to go back where you left or start from here.

An Overview of OOPs in Python

There are different ways to write a program to conquer a problem . Also , there are different paradigm under which we classify programming languages . One way is object oriented programming(OOPs).

To study more about programming paradigms , click here.

Python is a multi-paradigm programming language . It supports both procedural programming ( writing function step by step to achieve desired output like in C language ) and object Oriented programming.
In OOPs , we work on class and objects.

Class

A class is a template or structure or prototype written by the programmer for creation of objects.

A class contain sets of attributes (features of the class) and methods( functions that manipulate attributes or use them to achieve some output ) that are common to all the objects of this class.

Example : –

Consider a circle class , a class that represent the structure or template of the circles . A circle consists of features like radius ,diameter ( 2*radius). Consider an extra feature say color.

Under methods , we can define Perimeter (2*pi*r) and Area (pi*r*r) of the circle class.

Example of class
Example of class

Instance (Or Object)

An object is the instance of the class . Every object of the class holds different memory position in the program .

Example :- Consider the example of circle class, we define two objects C1 and C2 for class circle . One with color attribute set to aqua blue and other with color yellow .

The radius attribute is set to 190 mm and 125 mm respectively.Similarly , we can create as many object as needed and find perimeter or area for each of them.

Objects Of Class
Objects Of Class

Creating a Class in Python

A class in python is created in same way as function in python with some tweaks.

class clause is used to create a class in python . Naming convention for class name is generally UpperCamelCase (first letter of every word should be capital).

SYNTAX

class MyClass:

doc string # optional

Statement 1

Statement 2

Statement 3

…………………..

…………………..

Statement N

The statement includes class variables, class methods , instance variables etc which we will be discussing in later section.

Empty Class and the need of the Constructor

Let us create an empty Student class.

>>> class Student:
...     pass
... 
>>>
>>> Student
<class '__main__.Student'>

Now , we can create instances for this Student class .

>>> S1 = Student()
>>> S2 = Student()
>>>
>>> print(S1)
<__main__.Student object at 0x7fc3b3396b80>
>>> print(S2)
<__main__.Student object at 0x7fc3b1dfefd0>
>>>

S1 and S2 are two different objects of same class .

We can initialized some instance variables for S1 and S2.

>>> S1.Name = "JOHN"
>>> S1.Age = 17
>>> 
>>> S2.Name = "PETER"
>>> S2.Age = 18
>>> 
>>> print(S1.Name)
JOHN
>>> print(S2.Name)
PETER
>>> 

Similarly , we can create various student objects and initialized the value of name and age for them .

But as the number of student increases the above process of initializing variables will become a difficult task and more prone to errors.

To resolve this issue , the constructor came into play .

Constructor of the Class

A constructor is used to initialize the data members of the object when it is created.

SYNTAX

class Myclass:
def __init__(self,args*):
…………
body of constructor
………….

TYPES OF CONSTRUCTOR

1. Default Constructor

When we don’t declare __init__ function in the class , it become default constructor . The python implicitly took care of this scenario and create the class.

>>> class Student:
...     name = 'Student'
... 
>>> S1 = Student()
>>> print(S1.name)
Student
>>>

In the above example , there is no __init__ methods yet python interpreter doesn’t show any error because this is a valid way to declare a class.

2. Parameterized Constructor

It contains some more arguments other than self.

self is always the first argument.

>>> class Student:
...     def __init__(self,name):
...         self.name = name 
... 
>>> S1 = Student('John') 
>>> print(S1.name) John 
>>> S2 = Student('Peter') 
>>> print(S2.name) Peter 
>>>

In the above example , name is passed as an argument when object is created and the instance variable is initialized by the constructor.

3. Non - Parameterized Constructor

It doesn’t contain any argument other than self ( reference to the instance being created).

>>> class Student:
...     def __init__(self):           
...         self.Name = "Student"
...
>>> S1 = Student()
>>> print(S1.Name)
Student
>>

4. Python Trick : What if we create more than one constructor for a class ?

Let us create three constructor for a class.

>>> class X:
...     def __init__(self):
...             print('I am Ist')
...     def __init__(self):
...             print('I am Second')
...     def __init__(self):
...             print('I am Third')
... 
>>> x1 = X()
I am Third
>>>

Hence , the instance always called the last constructor during instantiation.

Python doesn’t has the concept of Constructor Overloading.

Conclusion

Now , you are familiar with some terminologies like class,instance, constructor etc . We can proceed further to understand members of the class . In the next tutorial, we are going to focus on the variables and methods of the class and their types .

Last but not the least, do leave your feedback and suggestions. We would love to hear your feedback.

Till then , keep coding.