OBJECT ORIENTED PROGRAMMING IN PYTHON

Share via:

OBJECT ORIENTED PROGRAMMING IN PYTHON

Object-Oriented Programming (OOP) is a powerful paradigm that allows developers to structure their code in a more organized and modular way.
In Python, OOP is implemented through classes and objects, which encapsulate data and behavior into reusable components.
Python is an object-oriented language, and it supports all the features typically associated with OOP, such as encapsulation, inheritance, and polymorphism.
Fundamental concepts of Object-Oriented Programming (OOP).
1. Class
2. Object
Classes:
A class is a blueprint for creating objects.
It defines the attributes (data) and methods (functions) that all objects of that class will have.
Classes provide a way to create user-defined data structures with their own properties and behaviors.
In Python, a class is defined using the class keyword.
Ex: To create a class named student

Objects:
An object is an instance of a class.
It is a concrete realization of the class blueprint, with its own unique data and behavior.
Objects are created based on the structure defined by the class. Multiple objects can be created from the same class, each with its own distinct set of attributes and methods.
Multiple objects can be created from the same class, each with its own distinct set of attributes and methods.
In Python, objects are created by calling the class as if it were a function.
Ex: To create one object of the class named student

Example

Explanation of the above example
Created a class named student with attributes names and grades.
This class student has two methods.
1. cal_avg_grade ( ) to calculate the average of the student
2. is_passed ( ) to check whether the student is passed or not based on the average of the grade. In our class it is 60 %.
In this class we created two objects named student1 and student2 with names vamsi and akash.
In Python, self and __init__() are special features used within classes, primarily to handle object instances and initialization.
About self in Class
It is a reference to the current instance of the class. It is used to access the attributes and methods of the object within the class definition.
In Python, all instance methods within a class must have self as their first parameter. When a method is called on an object, Python automatically passes the object itself as the self argument.
Within the class definition, self is used to access the object’s attributes and methods. It allows methods to operate on the object’s data.
About  __init__ 
It is a special method in Python classes that serves as the constructor. It is automatically called when an object is created from the class.
This method initializes the object’s attributes with specified values. It allows you to set up the initial state of the object when it is instantiated.
It can accept parameters to customize the initialization process. These parameters are passed when creating an object and used to initialize the object’s attributes.
Any initialization logic, such as setting default attribute values or performing setup operations, can be placed within the __init__() method.

 

 

Author    : Venkat Vinod Kumar Siram 
LinkedIn : https://www.linkedin.com/in/vinodsiram/
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates.
KTExperts is always active on social media platforms.
Facebook  : https://www.facebook.com/ktexperts
LinkedIn    : https://www.linkedin.com/company/ktexperts/
Twitter       : https://twitter.com/ktexpertsadmin
YouTube   :  https://www.youtube.com/c/ktexperts
Instagram  : https://www.instagram.com/knowledgesharingplatform
Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

Add Comment