About python decorators

What is decorate

A function that takes one function as input and returns another. Use when you want to do something before or after a function.

Example of use

Example


#Decorator
def test(func):
    def new_func(*args, **kwargs):
        print("start")
        result = func(*args, **kwargs)
        print("end")
        return result
    return new_func

#Decorator target
#Decorated by annotating
@test
def greeting():
    print("Hello")


greeting()

result


start
Hello
end

Example


#About decorators
#dump is called and func=Run as double
def dump(func):
    "Display input arguments and output values"
    def wrapped(*args, **kwards):
        print("Fuction name: %s" % func.__name__)
        print("Input arguments: %s " % ' '.join(map(str, args)))
        print("Input keyaeorods: %s " % kwards.items())
        output = func(*args, **kwards)
        print("Output:", output)
    return wrapped

@dump
def double(*args, **kwards):
    "Double every arguments"
    output_list = [2 * args for arg in args]
    output_dict = {k: 2 * v for k, v in kwards.items()}
    return output_list, output_dict

result


Fuction name: double
Input arguments: 3 5
Input keyaeorods: dict_items([('first', 100), ('next', 98.6), ('last', 40)])
Output: ([(3, 5, 3, 5), (3, 5, 3, 5)], {'first': 200, 'next': 197.2, 'last': 80})

References

"Introduction to Python 3" (Author: Bill Lubanovic)

Recommended Posts

About python decorators
About Python decorators
About python slices
About python comprehension
About Python tqdm.
About python, class
About python inheritance
About python, range ()
About python reference
[Python] About multi-process
About Python for loops
Summary about Python scraping
About function arguments (python)
About Python, for ~ (range)
About Python3 character code
[Python] Memo about errors
About Python development environment
Python: About function arguments
Python, about exception handling
About Python Pyramid traversal
About Python3 ... (Ellipsis object)
[Python] Chapter 01-01 About Python (First Python)
[Python] About standard input
About __all__ in python
[Python] Find out about pip
About Fabric's support for Python 3
Python
About python objects and classes
About Python variables and objects
About the Python module venv
Think about architecture in python
About python beginner's memorandum function
About the ease of Python
About the enumerate function (python)
About various encodings of Python 3
About Python, len () and randint ()
About Perl, Python, PHP, Ruby
A memorandum about Python mock
About Python string comparison operators
About Python and regular expressions
About the features of Python
About "for _ in range ():" in python
About Python and os operations
Python # About reference and copy
About Python sort () and reverse ()
A note about [python] __debug__
Initializing global variables using Python decorators
A good description of Python decorators
[Python] Let's write briefly about comprehensions
About python dict and sorted functions
About dtypes in Python and Cython
[Python] What is @? (About the decorator)
What was surprising about Python classes
About Python pickle (cPickle) and marshal
[Python] What are @classmethods and decorators?
[Python] About Executor and Future classes
About Python, from and import, as
I learned about processes in Python
About the basics list of Python basics
A note about mock (Python mock library)
kafka python