Created
March 7, 2023 02:56
-
-
Save jftuga/959a6d2566d8cbde94dc739fed941605 to your computer and use it in GitHub Desktop.
Python Decorator
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
# source: https://www.pythontutorial.net/advanced-python/python-decorators/ | |
def currency(fn): | |
def wrapper(*args, **kwargs): | |
result = fn(*args, **kwargs) | |
return f'${result}' | |
return wrapper | |
@currency | |
def net_price(price, tax): | |
""" calculate the net price from price and tax | |
Arguments: | |
price: the selling price | |
tax: value added tax or sale tax | |
Return | |
the net price | |
""" | |
return price * (1 + tax) | |
print(net_price(100, 0.05)) # output: $105.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment