Skip to content

Instantly share code, notes, and snippets.

@BurhanH
Created March 1, 2019 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BurhanH/eae851ffc2e43ab141daa39edbc2b51a to your computer and use it in GitHub Desktop.
Save BurhanH/eae851ffc2e43ab141daa39edbc2b51a to your computer and use it in GitHub Desktop.
decorators, python
def null_decorator(func):
return func
def uppercase(func):
def wrapper():
original = func()
modified = original.upper()
return modified
return wrapper
def strong(func):
def wrapper():
return '<strong>' + func() + '</strong>'
return wrapper
def emphasis(func):
def wrapper():
return '<em>' + func() + '</em>'
return wrapper
@null_decorator
def greet():
return 'Hello!'
print(greet())
# Hello!
@uppercase
def greet():
return 'Hello!'
print(greet())
# HELLO!
@strong
def greet():
return 'Hello!'
print(greet())
# <strong>Hello!</strong>
@strong
@emphasis
@uppercase
def greet():
return 'Hello!'
print(greet())
# <strong><em>HELLO!</em></strong>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment