-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetters_Setters_Deleters.py
More file actions
executable file
·62 lines (43 loc) · 1.57 KB
/
Getters_Setters_Deleters.py
File metadata and controls
executable file
·62 lines (43 loc) · 1.57 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
#Property Decorators - Getters , Setters , Deleters :
class Employee: #class is the blueprint for creating the instances
num_of_emolyee =0
raise_amount = 1.04
def __init__(self,first,last): # METHOD 1
self.first =first
self.last = last
#self.pay =pay
#self.email = first + '.' + last + '@email.com'
Employee.num_of_emolyee += 1
@property
def fullname(self):
return '{} {}'.format(self.first,self.last)
@property
def email(self):
return '{}.{}@email.com'.format(self.first,self.last)
#setting the full name by the setter for
# emp1.fullname = 'JP PAL'
@fullname.setter
def fullname( self, name):
first , last = name.split(' ')
self.first = first
self.last = last
@fullname.deleter
def fullname(self):
print('Delete Name!')
self.first = None
self.last = None
emp1 = Employee('JP', 'PAL')
emp1.fullname = 'JP PAL' #changing the fullname
emp1.first = 'Jay' # here first name is changed but the eamil is same , so fixing this issue by property decorators
print(emp1.first)
print(emp1.last)
print(emp1.email)
#print(emp1.fullname()) , run this if not using @property decorator
print(emp1.fullname) #defining it as a method but accessing as a attribute
del emp1.fullname # () not used bcz accesing as a attribute
print(emp1.fullname)
'''
Note, Normally Python doesn't allow you to have multiple methods with the same name
and different number of parameters. However,
in this case Python allows this because of the decorators used.
'''