Juan Nogales

hace 2 años ·

Publicaciones de Juan
>
How to use decorators Python

How to use decorators Python

oR
DECORATORS

Intermediate

 

@DECORATORS

Also called metaprogramming because
one part of a program tries to modify
(elgle](aT-Tgt oTela ol Md al=N olfele (eo laa No Vigigle]
compilation.

#lLet's review functions

def un(greeting): . LRT
print(greeting) —_—

functions can be passed as ar-
guments to avother function.
Those functions that take
other functions as arguments
are also called higher-order
Saal

un("Hello")

deux un .
deux ("Hello")

output

Hello
Hello

follow @writecode4

 

@DECORATORS

#An example of higher-order-functions

def frst(a): CIT Rai
PE

define a higher-order
def snd_hghr (func, a): aT
result = func(a) —
result
mvoke and assign a

final = snd_hghr(frst,3) NER Ral

print(final) =.

output

9

follow @writecode4

 

@DECORATORS

#Also, functions can return other functions

def frst(a): CTT RIT ti
def frst_square(): nesting another func-
a aC

print (frst_square)

frst(4) ) )
“ wvoke the first one

output

16

follow @writecode4

 

@DECORATORS

#lLet’s create a decorator

A RT i
def frst(func): _ Ea
vest avother
fuvction

%

def frst_square():
print (”Decorator”)
pS Q)
frst_square
ANT Ae
[15 VA [-Y-1 of @ J i
print ("Main")
my_decor = frst(my_decor)
my_decor()

output

Decorator
Main

follow @writecode4

 

@DECORATORS

#Use the decorator syntax

defin Sn )
def frst(func): _ me a function

def frst_square(): nesting another

FT aly
print (”Decorator”) iar
frst_square
Gila using a decorator syntax

Ed rE =
IS TRAGER UI)

ISR ERTZES a REV 712 STRAT VT
to implement decorators.

output

Decorator
Main

follow @writecode4

 

@DECORATORS

#Use multiple Decorators

def frst(func):
def frst_square(*args, **kwargs): \
print (”’Decorator”)

func()

i143 A a
frst_square LRP

def scnd(func):
def frst_square(*args, Rwargs): \
print (”2nd Decorator”)
func()

frst_square define other function

@frst .

@scnd

def my_decor():
print("Main")

using multiple decorators

output
Decorator

2nd Decorator
Main

follow @writecode4

#pythondeveloper #python

Ciencia y Tecnología
Comentarios

Más artículos de Juan Nogales

Ver publicaciones
hace 2 años

Trust

Juan Nogales ·

Te pueden interesar estos trabajos

  • Trabajar en empresa

    Sr Marketing BI Analyst

    Despegar

    En el · **#EquipoDespegar · somos un grupo de grandes profesionales dedicados a crear las mejores experiencias para que los viajes enriquezcan la vida de las personas. Trabajamos en equipo, nos involucramos, no nos da miedo equivocarnos y siempre vamos por más. · **#EquipoDespega ...

    Buenos Aires, Buenos Aires Province

    hace 2 días

  • Trabajar en empresa

    Analytics Engineer

    Despegar

    Sombrerete a un equipo único en el mercado que le gusta resolver problemas complejos e innovar. · Son protagonistas de su desarrollo profesional y buscan potenciar al máximo su talento. · Son centralizadores de información y aspiran a generar soluciones de alta calidad. · ...

    Buenos Aires

    hace 1 mes

  • Trabajar en empresa

    Sr Marketing Performance Analyst

    Despegar

    Serás responsable de hacer recomendaciones para mejorar el producto landing y optimizar la experiencia del usuario, así como realizar análisis técnico y cualitativo del producto landing y SEO de competidores. · Son centralizadores de información y aspiran a generar soluciones de ...

    Buenos Aires

    hace 1 mes