Skip to content

Commit acc7386

Browse files
committed
Property decorator and getters and setters
1 parent 4904749 commit acc7386

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/decorators_getters_setters.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#About the property decorator-Tutorials Corey Schafer
2+
3+
class Employee:
4+
def __init__(self,first,last,pay):
5+
self.first= first
6+
self.last = last
7+
self.pay = pay
8+
# self.email = first.lower() + '.' + last.lower() + '@mobiusservices.in'
9+
10+
#add email()
11+
@property
12+
def email(self):
13+
return '{}.{}@mobiusservices.in'.format(self.first,self.last)
14+
15+
@property
16+
def fullname(self):
17+
return '{} {}'.format(self.first,self.last)
18+
19+
@fullname.setter
20+
def fullname(self,name): #name is the value we are trying to set(fullname)
21+
first , last = name.split(' ')
22+
self.first=first
23+
self.last=last
24+
#Now this starts working print the fullname and all and check also check direct assign error solved
25+
26+
#We can also make a dletor in same way
27+
28+
@fullname.deleter
29+
def fullname(self):
30+
print('Delete name!')
31+
self.first=None
32+
self.last=None
33+
34+
35+
36+
emp1 = Employee('Vipulkumar','Yadav',500000)
37+
emp2 = Employee('Saurabh','Tiwari',100000)
38+
# emp1.first='Jim'
39+
emp1.fullname = 'Vipul Yadav' #error cant set attribute
40+
#in order to run above line sucessfully we need to use setter property
41+
#We want that as setting this fullname we want to auto change the first and last name and email
42+
43+
print(emp1.first)
44+
# print(emp1.email)
45+
# print(emp1.email())
46+
print(emp1.email)
47+
# print(emp1.fullname())
48+
print(emp1.fullname)
49+
#After adding deleter run below
50+
del emp1.fullname
51+
print(emp1.fullname)

0 commit comments

Comments
 (0)