Python is a high-level, dynamic, and interpreted programming language. It is a widely used general-purpose programming language. Python is a highly high-level language with dynamic semantics. It supports descriptive syntax too.
Python is a dynamic, open-source programming language that’s used by many developers. It has a very simple syntax and can be used for many different purposes, such as web development and data analysis.
This article will cover some of the more common features of Python revolving around the @ symbol.
More About the @ symbol
The @ symbol represents an operator. For example, @ = is the assignment operator, which allows you to assign one value to another variable or literal value. The colon (:) is used in Python as a separator between statements.
The most common Python decorators are:
- @property
- @classmethod
- @staticmethod
When @ symbol is used at the beginning of a line, it is used for class and function decorators, whereas when it is placed in the middle of a line, it means matrix multiplication (a binary operator).
Decorator functions
The decorator’s function decorates classes and functions. A decorator can be used to modify existing code without modifying the original code itself. Decorators are useful in several situations, including testing, logging, tracing, profiling, and more! A decorator accepts a function, adds some functionality, and returns it.
The fancy @ symbol is an essence of metaprogramming (through which you can manipulate the data within your own program.) These higher-order functions don’t let you change the source code but make it more convenient.
Uses
A decorator has multiple uses:
- Decorators are useful in several situations, including testing, logging, tracing, profiling, and more.
- It may suggest for how long a function will run.
- It suggests the number of times a function will execute.
Implementation of Decorator in a Function
Let us try to understand the functioning of decorators with an easy example.
def f1(func):
def wrapper_function():
print("1")
func()
print("No, Print me!!")
return wrapper_function
def f2():
print("3")
var = f1(f2)
Here, in order to obtain the output, the variable, var will pass the entire 2nd function, f2, as the argument for the first function, f1. Post this, the first 1 will be printed, then the 2nd function is called, and its print function is executed i.e. next, you will get 3 on screen. Lastly, you will return to the wrapper function and print 2.
132 #will be the output
Now, in the 2nd example, we will use a decorator with @ symbol to reduce the complexity of the entire function.
@func1
def func2():
print("Gfg")
#is same as
def func2():
print("Gfg")
func2 = func1(func2)
Class decorator functions using @ symbol
These functions are used at the start of a Python statement. They can be used in 3 places: property, classmethod, or staticmethod.
@classmethod allows you to create methods in classes without defining them yourself. This is possible by using self-defining classes. Similar classes exist in Java or other languages that have methods described within their classes. There is a need to be specified explicitly within those classes themselves. This happens because they’re part of their parent class. In doing so, it allows for better encapsulation.
@staticmethod works with the instance; it doesn’t affect the class. You can’t use self and cls as the arguments when creating a decorator as a static method. As a result, opt for the static method only when you create an instance of the class.
def extend_behavior(func):
return func
@extend_behavior
def some_func():
...
Try to understand this with another example
class PoW(object):
def __init__(self, arg):
self._arg = arg
def __call__(self, a, b):
ans = self._arg(a, b)
return ans ** 2
@PoW
def multiply_together(a, b):
return a * b
Matrix multiplication using @ symbol
You might find the @ symbol in the middle of a Python statement. This might be because you should understand that it will be used for matrix multiplication. It was introduced in PEP465. As per the code, this code invokes __matmul__ and __rmatmul__.
# x@y is similar to x.__matmul__(y)
# it will give the same answer as dot(x, y)
#example:
import numpy as np
X = np.matrix('1 2; 3 2')
Y = np.matrix('1 2; 1 2')
print(X@Y)
Inplace matrix multiplication using @=
symbol
If you wish to store the matrix multiplication result at a consolidated place, aka the same matrix, you can use inplace matrix multiplication.
x @= y # is same as x = dot(x,y)
@ symbol in Inner and Outer Product(Numpy)
This symbol also helps in calculating the inner and outer product in numpy. Check this example:
from numpy import array, matrix
array([[1,2,3]]).T @ array([[7,8,9]])
See also: Numpy outer() Method Explained In-Depth With 5 Examples(Opens in a new browser tab)
FAQs
__matmul__, __rmatmul__ or __imatmul__ refer to @= in python.
@symbol helps in better and easier matrix multiplication now.
Conclusion
In conclusion, the Python @ symbol, also known as the “at” symbol, has several uses in the language. It is commonly used as a decorator in Python 3 to define class methods, as well as to define matrix multiplication in Python 3.5 and above. Additionally, the @ symbol is used in Python for decorators, matrix multiplication, and email addresses. Its versatility and ease of use make it a valuable tool in Python programming.