-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial_6.py
More file actions
71 lines (59 loc) · 1.55 KB
/
tutorial_6.py
File metadata and controls
71 lines (59 loc) · 1.55 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Python Object Oriented Programming
class Employee(object):
def __init__(self, first, last):
self.first = first
# self.fname = first
self.last = last
# self.email = first + "." + last + "@company.com"
@property
def email(self):
# The property decorator helps to define the email method but it can be accessed just like an attribute
# instead of emp_1.email(), we use emp_1.email directly
return "{}.{}@company.com".format(self.first, self.last)
@property
def fullname(self):
return "{} {}".format(self.first, self.last)
@fullname.setter
# has to be same name as the method targetted
def fullname(self, name):
first, last = name.split(" ")
self.first = first
self.last = last
@fullname.deleter
# has to be same name as the method targetted
def fullname(self):
print("Delete name")
self.first = None
self.last = None
emp_1 = Employee("John", "Smith")
emp_2 = Employee("Steve", "Doe")
print(emp_1.first)
print(emp_1.email)
print(emp_1.fullname)
print()
"""
John
John.Smith@company.com
John Smith
"""
emp_1.first = "Jim"
print(emp_1.first)
print(emp_1.email)
# fullname can now be accessed as an attribute as oppose to previous tutorials where it was a class method
print(emp_1.fullname)
print()
"""
Jim
John.Smith@company.com
Jim Smith
"""
emp_2.fullname = "Jane Doe"
print(emp_2.first)
print(emp_2.email)
print(emp_2.fullname)
print()
del emp_1.fullname
print(emp_1.first)
print(emp_1.email)
print(emp_1.fullname)
print()