A good description of Python decorators

A decorator is a function that decorates a function that is a function for a decorator and replaces it with a decorated function.

For example, prepare the following function for the decorator.

def my_decorator(func):
    def decorated_my_func(*args, **kwargs):
        print("Decorated")
        func(*args, **kwargs)
    return decorated_my_func

Then decorate and declare an ordinary function with the above function.

@my_decorator
def my_func(str):
    print(str)

And when you run

>>> my_func("Ah ah ah")
Decorated
Ah ah ah

It becomes. That is, by adding @my_decorator when declaringmy_func ()

>>> my_func = my_decorator(my_func)
... my_func("Ah ah ah")
Decorated
Ah ah ah

Decorators do the same thing as.

Recommended Posts

A good description of Python decorators
A record of patching a python package
[Python] A memorandum of beautiful soup4
A brief summary of Python collections
A pharmaceutical company researcher summarized the basic description rules of Python
Display a list of alphabets in Python 3
Make a relation diagram of Python module
Connect a lot of Python or and and
[python] Get a list of instance variables
[python] [meta] Is the type of python a type?
A brief description of pandas (Cheat Sheet)
The story of blackjack A processing (python)
[Python] Get a list of folders only
A memorandum of python string deletion process
Introduction of Python
Basics of Python ①
Basics of python ①
Copy of python
About python decorators
About Python decorators
Introduction of Python
A memo of a tutorial on running python on heroku
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
Draw a graph of a quadratic function in Python
[python] Create a list of various character types
Get the caller of a function in Python
A memorandum of calling Python from Common Lisp
Make a copy of the list in Python
A memorandum of extraction by python bs4 request
Solve A ~ D of yuki coder 247 with python
Rewriting elements in a loop of lists (Python)
A note about the python version of python virtualenv
Make a joyplot-like plot of R in python
[Python] A rough understanding of the logging module
Output in the form of a python array
Get a glimpse of machine learning in Python
[Python] A rough understanding of iterators, iterators, and generators
A well-prepared record of data analysis in Python
A discussion of the strengths and weaknesses of Python
[Python] Implementation of clustering using a mixed Gaussian model
[Python] Operation of enumerate
List of python modules
A * algorithm (Python edition)
Experience the good calculation efficiency of vectorization in Python
[Python] A program that counts the number of valleys
A memo connected to HiveServer2 of EMR with python
[Python] Take a screenshot
Simulate a good Christmas date with a Python optimized model
(Python) Treat integer values as a set of flags
Recommendation of building a portable Python environment with conda
Create a Python module
Unification of Python environment
Copy of python preferences
Make a copy of a Google Drive file from Python
Cut a part of the string using a Python slice
A python lambda expression ...
Basics of Python scraping basics
A collection of code often used in personal Python
[python] behavior of argmax
How to write a list / dictionary type of Python3
Usage of Python locals ()