[Python] What is @? (About the decorator)

I'm working on a pyramid tutorial, but there are a lot of things I don't know.

For the time being, the sauce with @ came out. When using Java, I think it's an annotation, but That's not the case, and when I looked it up, it was a decorator.

Decorators are what you use to wrap an object. [Design pattern](http://en.wikipedia.org/wiki/%E3%83%87%E3%82%B6%E3%82%A4%E3%83%B3%E3%83%91%E3% 82% BF% E3% 83% BC% E3% 83% B3_% 28% E3% 82% BD% E3% 83% 95% E3% 83% 88% E3% 82% A6% E3% 82% A7% E3% It was 82% A2% 29), that's right.

So, it seems that Python provides that decorator as a feature. I wrote a simple source using a decorator.


#Decorator (before the function to decorate"before",behind"after"To output)
def deco_test(func):
    def wrapper(arg):
        print("before")
        func(arg)
        print("after")
    return wrapper


#Functions using decorators@Call the decorator using.
@deco_test
def hello(arg):
    print("Hello, " + arg)

hello("world!")

The execution result is as follows. before Hello, world! after

By the way, the hello function using the decorator can be replaced with the function below.

def hello(arg):
    print("Hello, " + arg)
hello = deco_test(hello)

I wasn't sure at first, but after trying various things, Python strongly learned that functions are also objects. The function is an argument. So, if you add () to the variable that stores the function and execute it.

It seems that you can write a program very flexibly, It's dynamically typed, and if you don't name it properly, It seems that a code that is very difficult to understand will be created.

By the way, the code that came out in the pyramid tutorial is as follows

@view_config(route_name='home', renderer='templates/mytemplate.pt')
def my_view(request):
    try:
        one = DBSession.query(MyModel).filter(MyModel.name == 'one').first()
    except DBAPIError:
        return Response(conn_err_msg, content_type='text/plain', status_int=500)
    return {'one': one, 'project': 'tutorial'}

I was a little worried because this view_config was the class name, The @ part is the instance creation, and the argument is passed to init. The decorating function my_view is wrapped in the instance call call.

I think that's what it means, but verification is coming again.

Recommended Posts

[Python] What is @? (About the decorator)
[python] What is the sorted key?
What is python
What is the python underscore (_) for?
What is Python
What is "mahjong" in the Python library? ??
[Python] What is Pipeline ...
[Python] What is virtualenv
A Java programmer studied Python. (About the decorator)
What is wheezy in the Docker Python image?
About the difference between "==" and "is" in python
What is the activation function?
About the Python module venv
About the ease of Python
About the enumerate function (python)
[Python] Python and security-① What is Python?
[Python] * args ** What is kwrgs?
What is the interface for ...
About the features of Python
What is the Callback function?
What is a python map?
Python Basic Course (1 What is Python)
What is Python? What is it used for?
[Python] What is a zip function?
[Python] What is a with statement?
Python Note: About comparison using is
What was surprising about Python classes
Python for statement ~ What is iterable ~
What is the X Window System?
What is the default TLS version of the python requests module?
Python> What is an extended slice?
About the basics list of Python basics
[Introduction to Python] What is the most powerful programming language now?
What to do when the value type is ambiguous in Python?
[Python] What is pandas Series and DataFrame?
What is NaN? NaN Zoya (Python) (394 days late)
[Python Kivy] About changing the design theme
What kind of programming language is Python?
Python learning basics ~ What is type conversion? ~
What is the ETL processing framework clivoa?
[Unix] What is the zombie process / orphan process?
What is the cause of the following error?
Where is the python instantiation process written?
Learn the design pattern "Decorator" in Python
[Python] Summarize the rudimentary things about multithreading
What is a dog? Python installation volume
[python] [meta] Is the type of python a type?
What about 2017 around the Crystal language? (Delusion)
[Machine learning] What is the LP norm?
About February 02, 2020 * This is a Python article.
What is namespace
About python slices
About python comprehension
Note: Python Decorator
What is copy.copy ()
[Python] What is a formal argument? How to set the initial value
What is the XX file at the root of a popular Python project?
About the test
[Introduction to Python] What is the difference between a list and a tuple?
What is dotenv?
About Python tqdm.