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

    Data Scientist SR

    Coderio | Software Company

    Sobre Coderio · Coderio diseña y entrega soluciones digitales escalables para empresas globales. Con una base técnica sólida y una mentalidad orientada al producto, nuestros equipos lideran proyectos de software complejos desde la arquitectura hasta la ejecución. Valoramos la aut ...

    Buenos Aires

    hace 4 días

  • Trabajar en empresa

    AI Tax Transformation Sr Specialist

    Despegar

    Se busca un especialista en transformación fiscal e inteligencia artificial para ejecutar y dar soporte a la implementación de soluciones tecnológicas avanzadas que automatice y optimicen los procesos impositivos mediante el uso de inteligencia artificial. · ...

    Buenos Aires, Buenos Aires Province

    hace 1 mes

  • Trabajar en empresa

    Technician I

    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. · #EquipoDespegar es crecer, desarrollarse, es proponerse un objetivo y no parar hasta alcanzarlo. Innovamos, creamos, ...

    Buenos Aires

    hace 1 mes