Site Tools


python:decorators

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
python:decorators [2024/08/30 16:22] – created, page about decorators. Nielspython:decorators [2024/09/30 08:09] (current) – Niels
Line 1: Line 1:
 ====== Decorators ====== ====== Decorators ======
  
-Decorators add behaviour to existing functions. They are bascially functions wrapping around the function being decorated. +Decorators extend functions, methods, classes without explicitly modifying the code of the item being decorated. 
  
-the implementation of a decorator takes care of passing arguments to a inner decorator function which defines the logic  +example of a decorator //Italic (skelleton structure only)//: 
-that the decorator is supposed to add and calls the original function. + 
 +<code python> 
 +def decorator_function(func): 
 +    ''' Receives the function being decorated as first argument''' 
 +     
 +    def actual_decorator(*args, **kwargs):  
 +        '''function that actually performs the decorated logic ''' 
 +         
 +        # do something, before original logic 
 +                 
 +        #call the original function         
 +        func(*args,**kwargs) 
 +         
 +        # and/or do something after.  
 +         
 +        #return result  
 +     
 +    #return the actual decorator so it gets called by calling context .  
 +    return actual_decorator    
 + 
 +</code> 
 + 
 +example of decorator with arguments//Italic (skelleton structure only)//: 
 + 
 +<code python> 
 +def decorator_function(a, b): 
 +    ''' Receives the decorator arguments''' 
 +     
 +    def additional_wrapper(func): 
 +        ''' receivs the function (and its arguments) being decorated ''' 
 +     
 +        def actual_decorator(*args, **kwargs):  
 +            '''function that actually performs the decorated logic ''' 
 +             
 +            #arguments a and b passed from outer fuction are in scope but read only.  
 +               if a > b: 
 +                   pass          
 +            # do something, before original logic                       
 +             
 +            #call the original function             
 +            func(*args,**kwargs) 
 +             
 +            # and/or do something after.  
 +             
 +            #return result 
 +         
 +        # return function to calling context (so that it gets executed).     
 +        return actual_decorator    
 + 
 +    #return fuction to calling context (so that it gets executed). 
 +    return additional_wrapper 
 +</code>
  
-Than the decorator returns a reference to its inner function. 
-So the decorator itself does not execute the decorator logic, the calling context does!  
  
 **Related:** **Related:**
Line 13: Line 62:
   *[[https://pybit.es/articles/decorators-by-example/ | Learning Python Decorators by Example (pybit.es)]]   *[[https://pybit.es/articles/decorators-by-example/ | Learning Python Decorators by Example (pybit.es)]]
   *[[https://wiki.python.org/moin/PythonDecorators| PythonDecorators (wiki.python.org)]]   *[[https://wiki.python.org/moin/PythonDecorators| PythonDecorators (wiki.python.org)]]
 +  *[[https://builtin.com/software-engineering-perspectives/python-class-decorator| Using Python Class Decorators (builtin.com)]]
 +  *[[https://realpython.com/python-metaclasses/| Python Metaclasses (realpython.com)]]((With metaclasses you can define how classes are created.))
 +  *[[https://realpython.com/primer-on-python-decorators/| Primer on Python decorators (realpython.com)]]
 +  *[[https://python-reference.readthedocs.io/en/latest/docs/functions/property.html | article on property decorator]]
 +   
  
 {{tag>python development decorators}} {{tag>python development decorators}}
  
python/decorators.1725027737.txt.gz · Last modified: by Niels