python decorator usage notes

What a decorator can do

You can use decorator to add processing before and after executing a function.

Once you have defined the decorator function, you can add processing just by writing @function name before the function you want to decorate, which is convenient when you want to write a lot of partially different functions or add a little processing.

decorator example

Below, we define the decorator function check_execution_time (), which measures the execution time of a function, and decorate the appropriate function test ().

python


import time

def check_execution_time(func): #func is a decorate target
    def wrapper(*args, **kwargs): #The argument to the decorate target goes here
        time1 = time.time() #Time before execution
        
        res = func(*args, **kwargs) #Execute the function received here
        
        time2 = time.time() #Time after execution
        erapsed_time = time2 - time1 #Calculate execution time
        return res, erapsed_time #Returns the execution result
    return wrapper

@check_execution_time #set decorator
def test(n): #Functions to be decorated
    sum_n = 0
    for k in range(n):
        sum_n += k
    return sum_n

sum_n, erapsed_time = test(1000000)
print('sum_k: %d' % sum_n)
print('erapsed_time: %1.3f' % erapsed_time)

output


sum_k: 499999500000
erapsed_time: 0.052

Let's try!

Recommended Posts

python decorator usage notes
[Python] pytest-mock Usage notes
Python standard unittest usage notes
Python scraping notes
Note: Python Decorator
Python study notes _000
Python learning notes
concurrent.futures Usage notes
Python beginner notes
python C ++ notes
Python study notes _005
Python grammar notes
Python Library notes
python personal notes
Python function decorator
python pandas notes
Python study notes_001
python learning notes
Python3.4 installation notes
missingintegers python personal notes
Python package development notes
Usage of Python locals ()
python decorator to retry
Python ipaddress package notes
[Personal notes] Python, Django
Python Pickle format notes
First Python miscellaneous notes
Matlab => Python migration notes
Notes around Python3 assignments
Notes using Python subprocesses
Python decorator operation memo
I tried Python> decorator
Python try / except notes
Python framework bottle notes
Python notes using perl-ternary operator
[Python] Correct usage of map
Web scraping notes in python3
Python notes to forget soon
Python notes using perl-special variables
Python 處 處 regular expression Notes
Python Tkinter notes (for myself)
Convenient diff command usage notes
[Python] Notes on data analysis
Python data analysis learning notes
Notes on installing Python on Mac
Python higher-order function (decorator) sample
Sample usage of Python pickle
Basic usage of Python f-string
Get Evernote notes in Python
[Python] Correct usage of join
Notes on installing Python on CentOS
Notes on Python and dictionary types
Decorator 1
Python
Minimum grammar notes for writing Python
[Introduction to Udemy Python 3 + Application] 57. Decorator
Notes on using MeCab from Python
[Python] What is @? (About the decorator)
Personal notes for python image processing
Python Pandas Data Preprocessing Personal Notes
Typing automation notes by Python beginners