In this blog, we will focus on oops in python i.e. is Object Oriented Programing language as before starting oops you must be clear in concepts of function and python different data types https://ainewgeneration.com/category/python/ There are various ways in which a problem can be solved using computer programming languages. The way to code a program in order to solve a problem may vary from developer to developer. One of these useful and popular ways( or programming paradigms) is Object-Oriented Programming. In this paradigm, the concepts, analogies, and entities of the real world are put to use in order to write programs and solve problems using them. In this blog, we will cover Object Oriented Programming in Python.
You might be thinking about how can the complex and living real-world concepts be implemented to do programming of a non-living architecture of a computer machine. Well, this is done by using the idea of classes and objects. Some of you might be familiar with classes and objects and some of you may not, either way, you should keep reading as this article will clear all your doubts and queries about Object-Oriented Programming and its fundamentals. So, let’s dive into the vast and interesting expanse of object-oriented programming.
Table of Contents
- OOPs concepts.
- Classes and Objects
- The four pillars of OOP
Python is a multiple paradigm language means it supports different programming paradigms like Functional Programming, Object-Oriented Programming, and Procedure Oriented programming
- Procedure Oriented Programming : when ever we work with python we define function over function suppose you want to develop a software application you will break down project to small small parts those small module will be function.
- Functional Oriented Programming : when you are using function inside the function to create an application as they are immutable once function are defined they can’t be changed .example Lambda Function.
- Object Oriented Programming : OOPs in python is all about creating an object. The main aim of oops is to binds together the data and function in one place.
Classes and Objects
Classes, in OOP, can be thought of as a blueprint, a prototype, or a template that has a set of similar things. Now, you might be thinking why are we associating a class with such names as a blueprint, a template, or a prototype. All these words try to convey the same meaning that is a class is that thing that sets a structure or a format for its instances (or as we call objects) to follow. Classes define a particular set of attributes and behavior which are common to all its objects. We can even say that all the objects of the same class are similar in some fashion, though they may have their own unique properties in addition to these as well.
Objects can thus be defined as real-world entities which have their own attributes (data variables in technical terms) and behavior (analogous to member methods). in other words objects will have something where you can store the data and their behaviors. if you want to store something in the object. Every object will have certain attributes and behaviors. for example, a person has a name, age, height, professional are the attributes and the Person is taking, dancing, singing, walking are defined their behaviors.
Methods in Python: The function in object-oriented programming are called methods Where you can define the behavior.
More details related to objects and classes https://ainewgeneration.com/python-objects-and-classes/
Let us now take an example to understand this in a better way. We can consider Employee as a class and various working employees namely Ram, Shyam, etc., as its objects or instances. Each of these entities has the characteristics of an Employee as each of them works and has a professional life. Also, they are all different from each other as they have their own personal characteristics and behavior as well which makes each of them different from each other.
Below is a short code in Python illustrating the concept of Classes and Objects.
class Employee: int id ram= Employee() shyam= Employee() ram.id=2 ram.name="Ram" shyam.id=4 shyam.name="Shyam"
The four pillars of OOP
There are four fundamental concepts on which the entire Object-Oriented Programming is based. So, these are called the pillars of OOP. These are Inheritance, Encapsulation, Polymorphism, and Abstraction. Let us now discuss each of these in detail, with respect to the Python programming language.
The virtue by which one class acquires, or as we say “inherits” the properties of another class is called Inheritance. The class whose properties are inherited is called Parent class or Base class while the class which inherits the properties is called Child class or Derived class. There are various types of inheritance: Single, Multiple, Multilevel, Hierarchical, and Hybrid to name a few. More details related to inheritance https://ainewgeneration.com/inheritance-in-python/
In python, inheritance can be implemented in the following manner:
class ParentClass(): def first(self): print("This is ParentClass function") class ChildClass(ParentClass): def second(self): print("This is ChildClass function") object1= ChildClass() object1.first() object1.second() output: This is ParentClass function This is ChildClass function
Encapsulation is the concept in OOP under which data and methods (that work on data) are wrapped in one single unit (called class). Let us understand this in simple terms by using real-world entities. Each of us must have seen a capsule. Just like a capsule hides all its underlying ingredients and details inside its outer layer of obscurity, in a similar way class, in OOP, encapsulates or wraps all the data and functions inside itself and this is called Encapsulation.
Encapsulation using private members double __ using double_underscores
we can’t access any variable which is define as prive variable with double underscores however if we want to access we can define getter and setter in the class.
class Fruit: def __init__(self, name, color): self.__name=name self.__color=color def display_fruit(self): print("Fruit Name:", self.__name) print("Fruit color:", self.__color) apple=Fruit("Apple", "red") #calling class method apple.display_fruit() #calling variable directly with give error apple.name output : Apple red AttributeError: 'Fruit' object has no attribute 'name'
The fundamental concept of Polymorphism lies in its name itself. ‘Poly’ means many and ‘morph’ means form. Thus, the literal meaning of polymorphism means to exist in many forms. This is quite relatable as all of us know that many things in nature and in our surroundings exist in various forms. The most common example of this is water (H2O). Water exists in three major forms in nature, which are solid(ice), liquid, and gas (water vapor).
The concept of polymorphism can be implemented in OOP by using various approaches like by overriding an attribute, a method, or even by using some special methods.
Method overriding refers to the practice of having more than one method with the same differing in their signature or parameters list or number and types of arguments.
The following syntax shows the definition of a method:
<access specifier><modifier><return type>method_name(parameter list)
In python, when we wish to call an already overridden method, then we make use of the super function.
The code for using this is as follows:
class Parent Class(object): def __init__(self): pass class ChildClass(ParentClass): def __init__(self): super().__init__()
Abstraction or Data Abstraction is the concept by which any program written using the paradigm of OOP, shows the user only relevant information thereby hiding all unnecessary details of its creation and implementation. The advantage of this is that it makes the program less complex by showing only necessary information.
For example, when we use the functions of math class in python or any other OOP language, we are only concerned with the value which we are passing and the result that we want. We are not at all interested in the inner details of its implementation like how this method works, what is its implementation, etc.
In the code down below, the user has no idea how the sqrt function of math class is working how does it calculate the square root, he/ she is just concerned with the result that it gives.
import math x= math.sqrt(25) print(x) output: 5.0
I hope this article must have given you a detailed idea about Object-oriented Programming in python. In our next blog, we will cover Logistic Regression in detail.