Created
November 20, 2020 22:30
-
-
Save PrudhviVajja/1d3ceae2f45ce47393f71d537278b2cd to your computer and use it in GitHub Desktop.
static 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 Multiply: | |
def __init__(self, numbers): | |
self.numbers = numbers | |
def __call__(self): | |
print(f"multiply {self.numbers}") | |
self.checker(self.numbers) | |
self.result = 1 | |
for num in self.numbers: | |
self.result *= num | |
print(self.result) | |
@staticmethod | |
def checker(numbers): | |
for num in numbers: | |
if type(num) != int: | |
raise Exception("Accepts only integers.") | |
valid = Multiply([1,2,3]) | |
invalid = Multiply([1,2,"three"]) | |
valid() | |
invalid() | |
# Outputs | |
😅 ››› python3 static_decorator.py | |
#Valid | |
multiply [1, 2, 3] | |
6 | |
# Not Valid | |
multiply [1, 2, 'three'] | |
Traceback (most recent call last): | |
File "medium.py", line 56, in <module> | |
invalid() | |
File "medium.py", line 40, in __call__ | |
self.checker(self.numbers) | |
File "medium.py", line 50, in checker | |
raise Exception("Accepts only integers.") | |
Exception: Accepts only integers. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment