Created
May 28, 2018 15:08
-
-
Save orasik/f51729e8e4259cb9a780b885d840654a to your computer and use it in GitHub Desktop.
Decorators example
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
def my_decortator(func): | |
def wrapper(*args): | |
print("Run before func") | |
func(*args) | |
print("Run after func") | |
return wrapper | |
def decorator_with_tags(tags): | |
def decorator(func): | |
def wrapper(name): | |
if tags == 'div': | |
return "<div>{}</div>".format(func(name)) | |
elif tags == 'p': | |
return "<p>{}</p>".format(func(name)) | |
else: | |
return "<h1>{}</h1>".format(func(name)) | |
return wrapper | |
return decorator | |
@my_decortator | |
def test_func(name): | |
print("Hello {}".format(name)) | |
@decorator_with_tags("div") | |
def test_tagged_decorator(name): | |
return name | |
test_func("World!") | |
print(test_tagged_decorator("My Name")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment