An @ symbol at the beginning of a line is used for class and function decorators:
PEP 318: Decorators
Python Decorators - Python Wiki
The most common Python decorators are:
@property@classmethod@staticmethod
An @ in the middle of a line is probably matrix multiplication:
@as a binary operator.
What is the '@=' symbol for in Python? - Stack Overflow
@ symbol in python
Purpose of @ symbols in Python? - Stack Overflow
What does the | symbol do in python?
Videos
An @ symbol at the beginning of a line is used for class and function decorators:
PEP 318: Decorators
Python Decorators - Python Wiki
The most common Python decorators are:
@property@classmethod@staticmethod
An @ in the middle of a line is probably matrix multiplication:
@as a binary operator.
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.
From the documentation:
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. @= is matrix multiplication followed by assignment, as you would expect. They map to __matmul__, __rmatmul__ or __imatmul__ similar to how + and += map to __add__, __radd__ or __iadd__.
The operator and the rationale behind it are discussed in detail in PEP 465.
@= and @ are new operators introduced in Python 3.5 performing matrix multiplication. They are meant to clarify the confusion which existed so far with the operator * which was used either for element-wise multiplication or matrix multiplication depending on the convention employed in that particular library/code. As a result, in the future, the operator * is meant to be used for element-wise multiplication only.
As explained in PEP0465, two operators were introduced:
- A new binary operator
A @ B, used similarly asA * B - An in-place version
A @= B, used similarly asA *= B
Matrix Multiplication vs Element-wise Multiplication
To quickly highlight the difference, for two matrices:
A = [[1, 2], B = [[11, 12],
[3, 4]] [13, 14]]
Element-wise multiplication will yield:
A * B = [[1 * 11, 2 * 12], [3 * 13, 4 * 14]]Matrix multiplication will yield:
A @ B = [[1 * 11 + 2 * 13, 1 * 12 + 2 * 14], [3 * 11 + 4 * 13, 3 * 12 + 4 * 14]]
Usage in Numpy
So far, Numpy used the following convention:
the
*operator (and arithmetic operators in general) were defined as element-wise operations on ndarrays and as matrix-multiplication on numpy.matrix type.method/function
dotwas used for matrix multiplication of ndarrays
Introduction of the @ operator makes the code involving matrix multiplications much easier to read. PEP0465 gives us an example:
# Current implementation of matrix multiplications using dot function
S = np.dot((np.dot(H, beta) - r).T,
np.dot(inv(np.dot(np.dot(H, V), H.T)), np.dot(H, beta) - r))
# Current implementation of matrix multiplications using dot method
S = (H.dot(beta) - r).T.dot(inv(H.dot(V).dot(H.T))).dot(H.dot(beta) - r)
# Using the @ operator instead
S = (H @ beta - r).T @ inv(H @ V @ H.T) @ (H @ beta - r)
Clearly, the last implementation is much easier to read and interpret as an equation.
Hi everyone,
I am not a programming expert but I am using pycharm to do one thing.
But let's get to the point.
I have created a "script" which is drawing from a .txt file to get the words to be written inside a text box, so I want my pc to write automatically.
but when I insert a "@" in the .txt file, the symbol is not written, do you know how I can solve it? can you help me?
Thanks in advance
They are called decorators. They are functions applied to other functions. Here is a copy of my answer to a similar question.
Python decorators add extra functionality to another function. An italics decorator could be like
def makeitalic(fn):
def newFunc():
return "<i>" + fn() + "</i>"
return newFunc
Note that a function is defined inside a function. What it basically does is replace a function with the newly defined one. For example, I have this class
class foo:
def bar(self):
print "hi"
def foobar(self):
print "hi again"
Now say, I want both functions to print "---" after and before they are done. I could add a print "---" before and after each print statement. But because I don't like repeating myself, I will make a decorator
def addDashes(fn): # notice it takes a function as an argument
def newFunction(self): # define a new function
print "---"
fn(self) # call the original function
print "---"
return newFunction
# Return the newly defined function - it will "replace" the original
So now I can change my class to
class foo:
@addDashes
def bar(self):
print "hi"
@addDashes
def foobar(self):
print "hi again"
For more on decorators, check http://www.ibm.com/developerworks/linux/library/l-cpdecor.html
They're decorators.
<shameless plug>
I have a blog post on the subject.
</shameless plug>
Hi, I saw the following line of code in a Python program which includes a symbol that I don't think I've come across before. Could someone please explain what this means? Thanks in advance.
print(x|y)