Criando Decorators em funções no Python

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World'
if __name__ == '__main__':
app.run()
def meu_primeiro_decorator(main_function):
def inner_function():
print('decorator')
return main_function()
return inner_function
@meu_primeiro_decorator
def funcao_com_decorator():
print('main_function')
decorator
main_function
def meu_primeiro_decorator_com_parametros(arg1):
def inner_function(function):
@wraps(function)
def wrapper(*args, **kwargs):
print('decorator')
print(f"Argument:{arg1}")
function(*args, **kwargs)
return wrapper
return inner_function
@meu_primeiro_decorator_com_parametros('arg1')
def funcao_com_decorator_com_parametros():
print('main_function')
decorator
Argument:arg1
main_function

--

--

Desenvolvedor Senior da empresa Imagem, MBA Arquitetura de Software

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store