Python Object Oriented Programming – Class and Object

By | August 20, 2015

In this tutorial we will discuss about Python Object Oriented Programming (OOP), class, object and constructor with some program examples.

Python is one of the few programming languages which supports both Structured Programming feature as well as Object Oriented Programming approach at the same time. So, in a single program we can have an OOP based section along with a Structured section.


Python Object Oriented Programming

Object-oriented programming (OOP) is a programming paradigm based on the concept of Classes and Objects. You can read more about OOPs features by following below link.

Object is actually data structure that contain both data and functions or methods, which helps to manipulate or work with that data.

Class is a blueprint on which object creation is based. Class is the base for objects. It is important for a class to be defined before creation of an object.

Objects are created or instantiated from class. One class can have mlutiple objects with different attributes. A program example is given below that shows how to define class and then create its objects in Python.


Python Class and Object Example



Python Object Oriented Programming 1


The first line declares the class name with a parameter ‘object’ that indicates that the class is based on an object datatype.

The next line is the declaration of a method (function) which the object can use later after instantiation. It has one parameter named as self.

As a matter of fact, every method must have a first special parameter as self that provides a way for a method to refer to the object itself.

This is followed by a set of statements in the function declaration.

Now, the main function comes into picture. Actually, we need not explicitly declare any main function in Python code. Python automatically understands the scope of main if indentations are proper.

We have declared a variable in main function that is assigned the class name. Actually this statement creates an object of that particular class.

Now, we can access the method within the class and this is done by the second statement of the main function.


Calling a Method

Following syntax is used to access the class methods using objects.



Python Constructor

A constructor is a special method that is automatically called as soon as you create an object. It is actually used to directly initialize the attributes (variables) of the class at the time of object creation. An example is given below that shows how constructor can be defined.




Python Object Oriented Programming 2


Here, __init__() method is used to define constructor.

In the above program, we have created two objects that are linked with the same class.

Lets take another example to understand the concept of class and object in Python.



Python Object Oriented Programming 3


If you found anything incorrect or have any doubts regarding above Python object oriented programming tutorial then mention it by commenting below.


Leave a Reply

Your email address will not be published. Required fields are marked *