-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmployee Class (More Inbuilt Methods).py
67 lines (51 loc) · 1.64 KB
/
Employee Class (More Inbuilt Methods).py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding: utf-8 -*-
"""Python 102 - Assignment 7
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1Gh1lGDY1Rxtsdug8ToudosZ85jXnalkA
"""
class Employee:
def __init__(self, name, id, salary, years_employed):
self.name = name
self.id = id
self.salary = salary
self.years_employed = years_employed
def __repr__(self) -> str:
string = f'Employee: (Name:{self.name}, ID:{self.id}, Salary:{self.salary}, Years_Employed:{self.years_employed})'
return string
def __str__(self) -> str:
string = ('\nEmployee Details:' +
'\nName:' + str(self.name) +
'\nID:' + str(self.id) +
'\nSalary:' + str(self.salary) +
'\nYears Employed:' + str(self.years_employed))
return string
def __lt__(self, object2):
return self < object2
def __gt__(self, object2):
return self > object2
def __le__(self, object2):
return self <= object2
def __ge__(self, object2):
return self >= object2
def __eq__(self, object2):
return self == object2
def __ne__(self, object2):
return self != object2
def __len__(self):
return len(self)
def __getitem__(self, key):
if key == 'name':
return self.name
elif key == 'id':
return self.id
elif key == 'salary':
return self.salary
elif key == 'years_employed':
return self.years_employed
a = Employee("H", 58649, 80000, 2.5)
b = Employee("J", 98245, 600000, 6)
print(a < b, a > b, a <= b, a >= b, a == b, a != b)
print("number of attributes: ", len(a), len(b))
print('attributes using dot notation: ', a.name)
print('attributes using subscript notation', a['name'])