[PYTHON] Make a function decorator

I want to create a decorator (with tag) that performs pre / post processing of the function as shown below and reuse it.

>>> @MyDecorator('any-tag')
... def foo():
...     print u'foo'
... 
>>> foo():

pre-action. tag=any-tag.
foo
post-action. tag=any-tag.

Make it like this.

class MyDecorator(object):
    
    def __init__(self, tag):
        self._tag = tag
    
    def __call__(self, f0):
        def decorated(*args, **kwargs):
            print u'pre-action. tag=' + self._tag
            ret = f0(*args, **kwargs)
            print u'post-action. tag=' + self._tag
            return ret
        return decorated

Version using functools.wraps.

Thank you @termoshtt (^ ◇ ^)

from functools import wraps

def MyDecorator(tag):
    def dec(f0):
        @wraps(f0)
        def decorated(*args, **kwargs):
            print u'pre-action. tag=' + tag
            ret = f0(*args, **kwargs)
            print u'post-action. tag=' + tag
            return ret
        return decorated
    return dec

Recommended Posts

Make a function decorator
How to make a recursive function
[Python] Make the function a lambda function
Python function decorator
Create a Python function decorator with Class
Make a squash game
Differentiate a two-variable function
Use a Property Decorator?
Make a distance matrix
I'll make a password!
Make a Nyan button
Decorate with a decorator
Make a Tetris-style game!
Make a Base64 decoder
[Practice] Make a Watson app with Python! # 2 [Translation function]
Make a function to describe Japanese fonts with OpenCV
[Python] Make sure the received function is a user-defined function
Make a Blueqat backend ~ Part 1
Create a function in Python
Make a Blueqat backend ~ Part 2
Write a kernel density function
How to call a function
Try creating a CRUD function
[Django] Make a pull-down menu
Make a LINE BOT (chat)
Let's draw a logistic function
Make a bookmarklet in Python
Make a fortune with Python
Make Responder a daemon (service)
Python higher-order function (decorator) sample
Let's make a rock-paper-scissors game
What is a callback function?
Easily cProfile with a decorator
Make a fire with kdeplot
Make a math drill print
Associate Python Enum with a function and make it Callable
Let's make a remote rumba [Hardware]
[Python] What is a zip function?
How to make a Japanese-English translation
Call a Python function from p5.js.
Make a Santa classifier from a Santa image
Decorator 1
Make a Tweet box for Pepper
Let's make a GUI with python.
Make a sound with Jupyter notebook
Let's make a spot sale service 2
Make a face recognizer using TensorFlow
A function that returns a random name
Let's make a breakout with wxPython
Let's make a spot sale service 1
How to make a crawler --Advanced
Make C compilation a little easier
python / Make a dict from a list.
Make a recommender system with python
Decorator 2
[Python3] Define a decorator to measure the execution time of a function
Python-dynamically call a function from a string
How to make a deadman's switch
[Blender] How to make a Blender plugin
Create a Python general-purpose decorator framework
Make Flask a Cloud Native application