Skip to content

Instantly share code, notes, and snippets.

@comwrg
Last active November 19, 2017 06:10
Show Gist options
  • Select an option

  • Save comwrg/8ef9a85fc9b3565c0f19baac8af0396c to your computer and use it in GitHub Desktop.

Select an option

Save comwrg/8ef9a85fc9b3565c0f19baac8af0396c to your computer and use it in GitHub Desktop.
python multi decorators
def makebold(fn):
    def wrapped():
        return "<b>" + fn() + "</b>"
    return wrapped

def makeitalic(fn):
    def wrapped():
        return "<i>" + fn() + "</i>"
    return wrapped

@makebold
@makeitalic
def hello():
    return "hello world"

print(hello()) ## returns "<b><i>hello world</i></b>"

equal to

def makebold(fn):
    def wrapped():
        return "<b>" + fn() + "</b>"
    return wrapped

def makeitalic(fn):
    def wrapped():
        return "<i>" + fn() + "</i>"
    return wrapped

# no decorators
def hello():
    return "hello world"

print(makebold(makeitalic(hello))())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment