-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest36_classproperty.py
More file actions
56 lines (44 loc) · 1.21 KB
/
test36_classproperty.py
File metadata and controls
56 lines (44 loc) · 1.21 KB
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
class Employee:
# special methods are surrounded by double '_'
def __init__(self, first, last):
self.first = first
self.last = last
#self.email = first + '.' + last + '@company.com'
# Changing fullname function to an attribute
@property
def fullname(self):
return f'{self.first} {self.last}'
# Creating a getter for attribute
@property
def email(self):
return f'{self.first}.{self.last}@company.com'
# Creating a setter function
@fullname.setter
def fullname(self, name):
first, last = name.split(' ')
self.first = first
self.last = last
# Creating a deleter function. This seems to work sort of like a destructor
@fullname.deleter
def fullname(self):
print('Delete Name!')
self.first = None
self.last = None
emp_1 = Employee('John', 'Smith')
print(emp_1.first)
print(emp_1.email)
#print(emp_1.fullname())
print(emp_1.fullname)
print()
emp_1.first = 'Jim'
print(emp_1.first)
print(emp_1.email)
#print(emp_1.fullname())
print(emp_1.fullname)
emp_1.fullname = 'Nana Karthi'
print()
print(emp_1.first)
print(emp_1.email)
#print(emp_1.fullname())
print(emp_1.fullname)
del emp_1.fullname