Created
March 1, 2019 18:44
-
-
Save BurhanH/eae851ffc2e43ab141daa39edbc2b51a to your computer and use it in GitHub Desktop.
decorators, python
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 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