Skip to content

Instantly share code, notes, and snippets.

@orasik
Created May 28, 2018 15:08
Show Gist options
  • Save orasik/f51729e8e4259cb9a780b885d840654a to your computer and use it in GitHub Desktop.
Save orasik/f51729e8e4259cb9a780b885d840654a to your computer and use it in GitHub Desktop.
Decorators example
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