Skip to content

Instantly share code, notes, and snippets.

@KianYang-Lee
Created September 4, 2022 13:41
Show Gist options
  • Save KianYang-Lee/6a8f4810b4e4ed0ffbc719d5530aad6f to your computer and use it in GitHub Desktop.
Save KianYang-Lee/6a8f4810b4e4ed0ffbc719d5530aad6f to your computer and use it in GitHub Desktop.
Gist for codes used in the article 'Understanding Decorator (@) in Python'
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