-
Notifications
You must be signed in to change notification settings - Fork 0
/
201_16_property_setter_decorator.py
48 lines (33 loc) · 1.37 KB
/
201_16_property_setter_decorator.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# we will discuss three problems in existing
# then we will solve them using getter, setter decorator
class Phone:
def __init__(self, brand, model_name, price):
self.brand = brand
self.model_name = model_name
self._price = max(price,0)
# self.complete_specification = f"{self.brand} {self.model_name} and price is {self._price}"
@property # when property decorater used you can used as a instance
def complete_specification(self):
return f"{self.brand} {self.model_name} and price is {self._price}"
# getter # setter
@property
def price(self):
return self._price
@price.setter
def price(self, new_price):
self._price = max(new_price,0)
def make_a_call(self, phone_number):
print(f"calling {phone_number} ...")
def full_name(self):
return f"{self.brand} {self.model_name}"
phone1 = Phone('nokia', '1100', 1000)
print(phone1._price)
print(phone1.complete_specification) # couse used property decorator
phone1._price = -500
print(phone1._price)
# print(phone1.complete_specification())
print(phone1.complete_specification) # couse used property decorator
phone1.price = -500 # this is only a better version of '_price'
print(phone1.price)
# print(phone1.complete_specification())
print(phone1.complete_specification) # couse used property decorator