The Python Oracle

What does the "at" (@) symbol do in Python?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Popsicle Puzzles

--

Chapters
00:00 Question
00:17 Accepted answer (Score 515)
00:53 Answer 2 (Score 431)
02:09 Answer 3 (Score 277)
02:53 Answer 4 (Score 263)
03:23 Thank you

--

Full question
https://stackoverflow.com/questions/6392...

Accepted answer links:
[PEP 318: Decorators]: http://www.python.org/dev/peps/pep-0318/
[Python Decorators]: http://wiki.python.org/moin/PythonDecora...
[@property]: http://docs.python.org/library/functions...
[@classmethod]: http://docs.python.org/library/functions...
[@staticmethod]: http://docs.python.org/library/functions...
[@]: https://stackoverflow.com/a/28997112/229...

Answer 3 links:
[PEP465]: http://www.python.org/dev/peps/pep-0465/

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #syntax #pythondecorators

#avk47



ACCEPTED ANSWER

Score 578


An @ symbol at the beginning of a line is used for class and function decorators:

An @ in the middle of a line is probably matrix multiplication:




ANSWER 2

Score 454


Example

class Pizza(object):
    def __init__(self):
        self.toppings = []

    def __call__(self, topping):
        # When using '@instance_of_pizza' before a function definition
        # the function gets passed onto 'topping'.
        self.toppings.append(topping())

    def __repr__(self):
        return str(self.toppings)

pizza = Pizza()

@pizza
def cheese():
    return 'cheese'
@pizza
def sauce():
    return 'sauce'

print pizza
# ['cheese', 'sauce']

This shows that the function/method/class you're defining after a decorator is just basically passed on as an argument to the function/method immediately after the @ sign.

First sighting

The microframework Flask introduces decorators from the very beginning in the following format:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

This in turn translates to:

rule      = "/"
view_func = hello
# They go as arguments here in 'flask/app.py'
def add_url_rule(self, rule, endpoint=None, view_func=None, **options):
    pass

Realizing this finally allowed me to feel at peace with Flask.




ANSWER 3

Score 323


In Python 3.5 you can overload @ as an operator. It is named as __matmul__, because it is designed to do matrix multiplication, but it can be anything you want. See PEP465 for details.

This is a simple implementation of matrix multiplication.

class Mat(list):
    def __matmul__(self, B):
        A = self
        return Mat([[sum(A[i][k]*B[k][j] for k in range(len(B)))
                    for j in range(len(B[0])) ] for i in range(len(A))])

A = Mat([[1,3],[7,5]])
B = Mat([[6,8],[4,2]])

print(A @ B)

This code yields:

[[18, 14], [62, 66]]



ANSWER 4

Score 274


This code snippet:

def decorator(func):
   return func

@decorator
def some_func():
    pass

Is equivalent to this code:

def decorator(func):
    return func

def some_func():
    pass

some_func = decorator(some_func)

In the definition of a decorator you can add some modified things that wouldn't be returned by a function normally.