-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython_oops_10.py
More file actions
30 lines (24 loc) · 846 Bytes
/
Python_oops_10.py
File metadata and controls
30 lines (24 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# DUNDER METHODS
class Employee:
def __init__(self, name, salary, role):
self.name = name
self.salary = salary
self.role = role
# creating dunder methods
def __add__(self, other):
return self.salary + other.salary
def __truediv__(self, other):
return self.salary / other.salary
# used to return class onject details
def __repr__(self):
return f"Employee('{self.name}', {self.salary}, '{self.role}')"
# generally used to display details of the constructor
def __str__(self):
return (f"The Name is {self.name}, Salary is {self.salary} and role"
f" is {self.role}")
emp1 = Employee("Nikhil", 5000, "Programmer")
# emp2 = Employee("Tarun", 1000, "Teacher")
# print(emp1 + emp2)
# print(emp1 / emp2)
print(emp1)
print(repr(emp1))