-
Notifications
You must be signed in to change notification settings - Fork 614
/
Copy pathP47_MoreOnDecorators.py
27 lines (22 loc) · 1020 Bytes
/
P47_MoreOnDecorators.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
# Author: OMKAR PATHAK
# In this example, we will be seeing some more concepts of decorators such as
# property decorator, getters and setters methods.
class BankAccount(object):
def __init__(self, firstName, lastName):
self.firstName = firstName
self.lastName = lastName
@property # property decorator
def fullName(self):
return self.firstName + ' ' + self.lastName
@fullName.setter
def fullName(self, name):
firstName, lastName = name.split(' ')
self.firstName = firstName
self.lastName = lastName
if __name__ == '__main__':
acc = BankAccount('Omkar', 'Pathak')
print(acc.fullName) # Notice that we can access the method for our class BankAccount without
# parenthesis! This is beacuse of property decorator
# acc.fullName = 'Omkar Pathak' #This throws an error! Hence setter decorator should be used.
acc.fullName = 'Jagdish Pathak'
print(acc.fullName)