Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm looking at some Python code which used the @ symbol, but I have no idea what it does. I also do not know what to search for as searching Python docs or Google does not return relevant results when the @ symbol is included.

like image 286
AJ00200 Avatar asked Jun 17 '11 23:06

AJ00200


People also ask

What does AT symbol do in Python?

Today you learned how the at symbol @ is used in Python. To recap, the main use for the @ symbol is decorators. A decorator is used to extend the functionality of a function, method, or class from outside.

What is at symbol in Python matrix?

The @ (at) operator is intended to be used for matrix multiplication. No builtin Python types implement this operator. The @ operator was introduced in Python 3.5.

What does .T mean in Python?

In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.

What does =+ mean in Python?

Dec 14, 2020. The Python += operator lets you add two values together and assign the resultant value to a variable. This operator is often referred to as the addition assignment operator.

What does the symbol @ mean in Python?

Let’s take a more in-depth look at both of these. The main use case of the symbol @ in Python is decorators. In Python, a decorator is a function that extends the functionality of an existing function or class. Here the extend_behavior decorator function extends the behavior of some_func.

What does the @ operator mean in Python?

If you are referring to some code in a python notebook which is using Numpy library, then @ operator means Matrix Multiplication. For example: Decorators were added in Python to make function and method wrapping (a function that receives a function and returns an enhanced one) easier to read and understand.

What is the use of @@@ in Python?

@ can be used for matrix multiplication and decorator in python. While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

What is the @ symbol at the beginning of a line?

An @ symbol at the beginning of a line is used for class, function and method decorators. Read more here: The most common Python decorators you'll run into are: If you see an @ in the middle of a line, that's a different thing, matrix multiplication.


2 Answers

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

Read more here:

PEP 318: Decorators

Python Decorators

The most common Python decorators you'll run into are:

@property

@classmethod

@staticmethod

If you see an @ in the middle of a line, that's a different thing, matrix multiplication. See this answer showing the use of @ as a binary operator.

like image 87
FogleBird Avatar answered Sep 19 '22 02:09

FogleBird


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.

like image 42
Morgan Wilde Avatar answered Sep 21 '22 02:09

Morgan Wilde