How Object-Oriented Programming Is Used In Python?
Since that we know OOPs was basically introduced in all other programming languages like java, c, c++ etc!! but later it was updated with python programming language as well.. So, OOPs which basically stands for Object-Oriented system or object-oriented programming and most popularly known programming paradigm.
The two main majorly focused programming languages are,
- Object-oriented programming.
- Procedure Oriented programming.
Let’s understand the key points about oops concept first which are as follows:
· Classes and objects
· Classes in python
· Oops concept in python
What are Classes and Objects In Python: The most common understanding about classes and objects goes with real-time examples which are easy things to remember which could be defined as classes as an exemplar of object / Objects are the variables of a class data type and A class is a user-defined framework or prototype from which objects are created.
What are classes in Python:- A class is an extensible program-code-template for creating objects.
Example: The class name is used as the name for the class, when an object is created by a constructor of the class, the resulting object is called an instance of the class, and the member variables specific to the object are called instance variable.
Object-Oriented Programming Concept in Python:-
Python allows different programming languages since it got its unique programming language to accept all different approaches applied in it. Whereby one of the easiest and most popular approaches to solve the program is by creating objects which are nothing, but Object-Oriented Programming in python.
· OOPs makes the program easy to understand
· The code can be reused since the class is sharable
· Where the data is always safe with data abstraction
An object has two characteristics:
· Attributes:- Data describing the object
· Behavior:- Behavior methods on the attributes
Let's understand the whole OOPs concept with simple examples applied in our daily life.
Example 1:
A Ostrich is an object, as it has the following properties:
name, age, color as attributes
eating, flying as behavior
The OOP in python reuses the code which is also known as a “DRY concept”.
The basic principle of OOP follows in python which is as follows:
A class is a representation of the object.
since we know that a Ostrich is an object where its colour, name, size features etc.. creates the image of Ostrich based on these features we create an image of Ostrich as an object.
The example of a class of Ostrich can be :
class Ostrich:
pass
An instance is a specific object created from a particular class so the class keyword is used to define as an empty class in this above example
Object:
To define about the object we need to understand few things that are object is not been allocated for any kind of storage or memory and only when class is defined only the description of the object is used..
The example for object of Ostrich class can be:
obj = Ostrich()
Here, obj is an object of class Ostrich.
lets understand how to define and class the Ostrich
Example 1: Creating Class and Object in Python
Example 1: Creating Class and Object in Python
class Ostrich:
class attribute
species = "bird"
Instance attribute
def __infit__(self, name, age):
self.name = name
self.age = age
Instantiate the Ostrich class
gre= Ostrich("Gre", 10)
zoo= Ostrich("zoo", 15)
Access the class attributes
print("Gre is a {}".format(Gre.__class__.species))
print("zoo is also a {}".format(zoo.__class__.species))
Access the instance attributes
print("{} is {} years old".format( Gre.name, Gre.age))
print("{} is {} years old".format( zoo.name, zoo.age))Output
Gre is a bird
zoo is also a bird
Gre is 09 years old
zoo is 14 years old
The above program, we created a class with the name Ostrich where the attributes are the characteristics type of an object.
These attributes are defined inside the “IN’IT” method of the class. It is the initializer method that is first run as soon as the object is created.
Then, we create instances of the Ostrich class. Here, Gre and zoo are references (value) to our new objects.
We can access the class attribute using __class__.species. Class attributes are the same for all instances of a class. Similarly, we access the instance attributes using zoo.name and zoo.age. However, instance attributes are different for every instance of a class.
Let's look about the main concept of OOPs i.e
Object-Oriented Programming methodologies:
Object-Oriented Programming methodologies deal with the following concepts.
· Inheritance
· Polymorphism
· Encapsulation
· Abstraction
Inheritance:
inheritance deals with the heritage of the history exactly, I mean in programming language aspect “ inheritance of characters from parent to child class without any modification” the one class which is created called parent class and the new value which are derived from it called as child class.
Example:
Create a class named Person, with first name and last name properties, and a print name method:
class Person:
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
#Use the Person class to create an object, and then execute the printname method:
x = Person("liza", "Doe")
x.printname()
output:- lizaDeo
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
#Use the Person class to create an object, and then execute the printname method:
x = Person(“liza”, “Doe”)
x.printname()
output:- lizaDeo
Polymorphism:- This is the only single concept in python which allows us to work with a single type entity like method, object and operator to work with different scenarios with different types...
Let’s take an example:
Example 1: Polymorphism in addition operator
We know that the +operator is used extensively in Python programs. But, it does not have a single usage.
For integer data types, +operator is used to performing arithmetic addition operation.
num1 = 1
num2 = 2
print(num1+num2)
Hence, the above program outputs 3.
Encapsulation: Its the best combination of data and actions with the same block of code where the class of an object is instantiated/observed.
we can simply think, where the group of data related to marketing, cannot be mixed with manufacturing team since the groups go hand in hand in order to get the info of another department, who is not permitted to work so, here it creates the new action taking the permission from the higher authority for the further process this is how encapsulation works.
Let's understand this concept with an example:
class Car:
class Car:
def __init__(self):
self.__updateAI()
def drive(self):
print('driving')
def __updateAI(self):
print('updating AI')
redcar = Car()
redcar.drive()
redcar.__updateAI() not accessible from object.
def __init__(self):
self.__updateAI()
def drive(self):
print(‘driving’)
def __updateAI(self):
print(‘updating AI’)
redcar = Car()
redcar.drive()
redcar.__updateAI() not accessible from object.
This program will give the following output: updating AI, driving
Composition: This concept works differently in python programing language, which gives the structure of the created object.
Let's take the example of the bike to define a bike we need to mention its special features, its characters, its brand name etc.. were the main parts like tiers and handle resembles what kind of bike we are thinking about, which is the composition of the bike.
Python Object Oriented Programming Advantages
Classes and inheritance reduce the amount of code you have to write and that is because it is modular.
Object-oriented programming helps you modularize your code into classes. Then you can reuse those classes in other programs and other modules, etc
Object-oriented programming
it is easier to understand, debug, maintain, and extend your code. If I want to add an additional function onto reptile, I can easily do that just using the reptile class. It doesn’t have to affect any of my other code. It’s easier to debug because I’m debugging a much smaller block of code. It’s easier to understand and maintain the code.
Conclusion:- since that we use OOPs concept in many different fields and applications its always advisable to understand this concept with the help of examples for easy memorization here concept with Inheritance, Polymorphism, Encapsulation, Abstraction applied in every field gives the different way of working system, since the class is sharable and codes are reused, also the data is safe and secured for the working process.
https://saurabhmirgane007.medium.com/what-are-r-squared-and-adjusted-r-squared-dbfc0961b864
Thank you:-)
Happy Learning…