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.
https://en.wikipedia.org/wiki/Object-oriented_programming
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
class BaseClass(object): def write(self): print "This is an Instance of BaseClass" # main BaseObject = BaseClass() BaseObject.write()
Output
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.
Syntax
Object_Name.Method_Name()
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.
Example
class BaseClass(object): #constructor def __init__(self): print "A New Object has been Created" #method def write(self): print "This is an Instance of BaseClass" # main BaseObject1 = BaseClass() BaseObject2 = BaseClass() BaseObject1.write() BaseObject1.write()
Output
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.
class Doctor: def __init__(self, docname, docsalary): self.docname = docname self.docsalary = docsalary def display(self): print "Doctor Name : ", self.docname, ", Salary: ", self.docsalary Doctor1 = Doctor("Dr. Neeraj", 120000) Doctor2 = Doctor("Dr. Tushar", 80000) Doctor1.display() Doctor2.display()
Output
If you found anything incorrect or have any doubts regarding above Python object oriented programming tutorial then mention it by commenting below.