Created
September 4, 2022 13:41
-
-
Save KianYang-Lee/6a8f4810b4e4ed0ffbc719d5530aad6f to your computer and use it in GitHub Desktop.
Gist for codes used in the article 'Understanding Decorator (@) in 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
import time | |
import typing | |
def add_two_integers(a: int, b: int) -> int: | |
return a + b | |
def add_two_integers(a: int, b: int) -> int: | |
start_time = time.time() | |
result = a + b | |
end_time = time.time() | |
print("Operation have taken: ", end_time - start_time, " second") | |
return result | |
def timing_decorator(func: typing.Callable): | |
def wrapper(a: int, b: int): | |
start_time = time.time() | |
func_output = func(a, b) | |
end_time = time.time() | |
print("Operation have taken: ", end_time - start_time, " second") | |
return func_output | |
return wrapper | |
@timing_decorator | |
def add_two_integers(a: int, b: int) -> int: | |
return a + b | |
result = add_two_integers(2, 3) | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment