Updated April 17, 2023
Introduction to Object in Python
Python is an object-oriented language, not like a procedure-oriented programming language where the main focus is on functions. Python emphasises objects and classes; objects are collections of variables and methods that operate on that data, and class is a blueprint for the object. We can create as many objects from a class; Objects are also known as an instance of the class, and creating an instance of a class is known as instantiation.
Syntax
class Class_Name:
.....................................
Object_Name = Class_Name()
Object_Name.Method_Name()
Example Program
Program for determination of performance percentage of the employee
Objective
- The objective of the program is to evaluate the performance of the employee.
- The average units produced by the employee and the time taken for the production of these units are keyed in, which in turnkeys out the employee’s performance value.
- So using this performance value, the rating of the employee is determined.
This program is intended to work on multiple inheritance subjects where the child class is responsible for calling the methods implied in its corresponding parent class.
#!/usr/bin/evn python
# Define a class as 'Individual' #
import sys
class Individual:
# Constructor#1 #
Valid_genders = ['Male','Female','transgender']
def __init__(self):
self.Employee_Name = input( " Enter Name of the Employee : " )
self.Employee_age = input( " Enter age of the Employee : " )
print( " Valid gender values are " )
print( " 1. Male " )
print( " 2. Female " )
print( " 3. Transgender " )
try:
self.Employee_gender = input( " Enter gender of the Employee : " )
if self.Employee_gender not in self.Valid_genders:
raise Exception('valueerror')
except Exception as valueerror:
print("PLEASE ENTER A VALID GENDER")
sys.exit(0)
def display(self):
print( " ! ! ! ! ! EMPLOYEE PERFORMANCE CALCULATOR ! ! ! ! ! " )
print( " Employee Name : " , self.Employee_Name )
print( " Employee Age : " , self.Employee_age )
print( " Employee Gender : " , self.Employee_gender )
# Define a class as 'Evaluated_Rating' #
class Evaluated_Rating:
# Constructor#2 #
def __init__(self):
self.department = input( " department of the Employee : " )
print( " Note : An employee produces more than 50 units with 7 average minutes perunit in a day " )
self.Productivity = int(input( " Average productive units by the employee per day : " ) )
self.production_time = int(input( " Average production time for one unit by the employee : " ) )
def display(self):
print( " Employee Deparment : " , self.department )
performance_percentage = (self.Productivity * self.production_time)/100
print( " Performance percentage : " , performance_percentage )
if (performance_percentage > 4 ) :
print(" THE EMPLOYEE HAS SCORED RATING 3 ")
elif (performance_percentage > 6 ) :
print(" THE EMPLOYEE HAS SCORED RATING 2 ")
elif (performance_percentage > 9):
print(" THE EMPLOYEE HAS SCORED RATING 1 ")
class Employee(Individual, Evaluated_Rating):
def __init__(self):
# Call ' Individual ' super class constructor
Individual.__init__(self)
Evaluated_Rating.__init__(self)
def result(self):
Individual.display(self)
Evaluated_Rating.display(self)
Employee1 = Employee()
Employee1.result() # object using the methods of the declared class
Employee2 = Employee()
Employee2.result() # object using the methods of the declared class
print(" ")
print( " Note : The instances get initialized with the given values Successfully " )
Output :
Explanation
The objective of the program is to evaluate the performance of the employee. Therefore, the average units produced by the employee and the time taken for the production of these units are keyed in, which in turnkeys out the employee’s performance value. So using this performance value, the rating of the employee is determined.
From the technical perspective, the objects ‘Employee1’ and ‘Employee2’ are instantiated, and the flow of the program is wisely controlled.
Below is the list of elements displayed by the program for a single employee,
Enter Name of the Employee :
Enter the age of the Employee :
Valid gender values are
- Male
- Female
- Transgender
Enter the gender of the Employee :
department of the Employee :
Average productive units by the employee per day :
Average production time for one unit by the employee :
! ! ! ! ! EMPLOYEE PERFORMANCE CALCULATOR ! ! ! ! !
Employee Name :
Employee Age :
Employee Department :
Performance percentage :
THE EMPLOYEE HAS SCORED RATING
The advantages of using Objects through object-oriented programming
- objects play a vital role in the reuse of code. This reuse is majorly achieved by means of the concept of inheritance in object-oriented programming.
- Polymorphism in object-oriented programming drags a greater extent of flexibility to the code.
- Problem-solving becomes a comparatively very easier task with the introduction of objects; this is achievable because of the objects’ code control flexibility.
Conclusion – Object in Python
The concept of object-oriented programming plays a handy role in implying efficient high-level programming in the python language. And these objects act as the key kingpins in the execution of these concepts.
Recommended Articles
We hope that this EDUCBA information on “Object in Python” was beneficial to you. You can view EDUCBA’s recommended articles for more information.