Created
November 11, 2020 07:51
-
-
Save PrudhviVajja/afc3aa7c9a22d282c0e7d4ade6eea697 to your computer and use it in GitHub Desktop.
property-decorator python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Storage: | |
""" | |
MaxLimit of storage is 100 | |
""" | |
def __init__(self, maxlimit=0): | |
self.limit = 100 | |
self.maxlimit = maxlimit | |
@property | |
def maxlimit(self): | |
""" Getting maxlimit value | |
Returns: | |
int: Maxlimit of storage | |
""" | |
return self._maxlimit | |
@maxlimit.setter | |
def maxlimit(self, value): | |
"""Setting Value | |
Args: | |
value ([int]): [New storage] | |
Raises: | |
Exception: [Storage limit Exceded] | |
""" | |
if value > self.limit: | |
raise Exception(f"Storage limit exceded. Box Max Limit => {self.limit}") | |
else: | |
self._maxlimit = value | |
box = Storage(45) | |
print(f"Box Limit => {box.maxlimit}") | |
box.maxlimit = 200 | |
print(f"Box Limit => {box.maxlimit}") | |
# Output | |
🏆 ››› python3 property_decorator.py | |
Box Max Limit => 100 | |
Box Limit => 45 | |
Traceback (most recent call last): | |
File "medium.py", line 29, in <module> | |
box.maxlimit = 200 | |
File "medium.py", line 22, in maxlimit | |
raise Exception(f"Storage limit exceded. Box Max Limit => {self.limit}") | |
Exception: Storage limit exceded. Box Max Limit => 100 | |
FAIL: 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment