Back to: Python Tutorials For Beginners and Professionals
OOPs in Python
In this article, I am going to discuss OOPs in Python with examples. Please read our previous article where we discussed Dictionary in Python with examples. As part of this article, we are going to discuss the following pointers which are related to OOPs in Python.
- Before OOPS
- Functional Oriented Programming
- Procedural Oriented Programming
- After OOPS
- Where does Python belong to?
- What are OOPS?
- Features of OOPS
- Class and Objects in Python
- Self variable in Python
- Can we create more than one object to a class?
Before oops(object oriented programming), there were two approaches, procedure-oriented programming and functional oriented programming, being used.
What are these?
All the three approaches mentioned above are programming paradigms. A programming paradigm is nothing but the style of writing a program. We can also call them as methodologies which are used in writing a software program. Different languages follow different methodologies.
Functional Oriented Programming
Functional oriented programming is a program paradigm where programs are built around functions. Functions can be used as parameters, can be returned, can be used in other functions etc. Here functions are everything and they should always return the same output for a particular input. Erlang, Haskell, and Sakaila are the languages which are built on this paradigm.
Procedural Oriented Programming
POP languages are C, Pascal, Fortran etc. These languages used procedures and functions to perform the task. This kind of programming divides the program into subtasks composed of several procedures or functions or routines. This approach is called Procedure Oriented Approach.
OOPs is paradigm or methodology to implement software. OOPs languages are C++, Java, Dot Net etc. While developing any software, by using OOPs we need to implement it using classes and objects. This approach is called Object-Oriented Programming.
Where does Python belong to?
Python follows both functional oriented and object oriented programming approaches.
What is OOPS?
Object Oriented Programming language is a methodology which uses the concept of class and object to program. Let’s understand the concept of classes and objects with an example of a vehicle.
For example, let’s list down the properties a vehicle possess
- Number of wheels
Now, if we consider ‘vehicle’ as class, then the properties which we listed above will be attributes of that class. The types of vehicles available are
These types of vehicles will be objects to the class vehicle. All the properties of a car, like the name of the vehicle, number of wheels it has, its colour, its brand can be stored in the class ‘vehicle’. In order to access the information of the car we can create and object for the class and access it. Let’s say ‘car_obj’ is the object created for class ‘vehicle’ and assume the following data
- car_obj.name will be car
- car_obj.number_of_wheels will be four
- car_obj.colour will be blue
- car_obj.brand will be audi
Similarly, let’s create another object with name bike_obj
- bike_obj.name will be bike
- bike_obj.number_of_wheels will be two
- bike_obj.colour will be black
- bike_obj.brand will be honda
For both the objects, car_obj and bike_obj, the class is the same i.e vehicle, the attributes are the same, but the data is different. You can create any number of objects to the class and use the attributes to save the data.
We can say that class is a template or blueprint with which we can create objects. The objects of the same class contain different data but the same attributes.
FEATURES OF OOPS:
Let’s understand the terms familiar in OOPS.
- Abstract class
Class in Python:
A class is a blueprint or template which contains attributes.It is a specification of properties and action of objects.
Defining a class:
Documentation string represents the description of the class. It’s up to you to include this i.e its optional. It’s a good programming practice to include this.
After the class keyword comes the name of the class. After the name of the class we should give colon : symbol. class can contain
Constructors are used for initialization purpose and variables are used to represent the data. Variables can be of three types are as follows:
- instance variables
- static variables
- local variables
Methods are used to represent actions. Methods are again of three types are as follows:
- instance methods
- static methods
- class methods
Example: Define a class (demo1.py)
class Employee: def display(self): print("Hello my name is Shiksha")
In the above program ‘Employee’ represents a class which is defined by the developer. display is the method in the class. The program doesn’t give any output because we haven’t created any object to access the class members (attributes).
Self variable in Python:
In the demo1.py the display method has a parameter named ‘self’. Let’s talk about it:
- self is a default variable that refers to a current class object or the current instance(object) of a class.
- Generally, by using self, we can initialize the instance variables inside the constructor. (To be discussed in detail later).
- By using a self variable, we can access instance variables and instance methods.
- It can also be some other name rather than ‘self’, but the most commonly used and preferred is ‘self’.
- It contains the address of the current object being referred.
Objects in Python:
An object is an instance of a class. Using objects we can access class members, store data etc. They are like physical entities of class.
Syntax: name_of_the_object = NameOfTheClass()
Example: demo1.py contd..
class Employee: def display(self): print("Hello my name is Shiksha") emp_obj = Employee() emp_obj.display()
Here, we created an object ‘emp_obj’ for the class ‘Employee’. With the created object, we called the display method which is printing something to the console. Therefore, after creating the object of a class, we can access the attributes available in that class with the created object as shown below.
Can we create more than one object to a class?
Yes, the main concept of OOPs is class acts as a template for the objects. So, any number of objects can be created for one class.
Example: Creating more than one object (demo2.py)
class Employee: def display(self): print("Hello my name is Rahul") emp_obj1 = Employee() emp_obj1.display() emp_obj2 = Employee() emp_obj2.display()
In the next article, I am going to discuss Constructors in Python. Here, in this article, I try to explain OOPs in Python. I hope you enjoy this OOPs in Python article. I would like to have your feedback. Please post your feedback, question, or comments about this article.