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

@ Symbol

Python is an excellent interpreted programming language and has its own syntax. The set of guidelines that specify how a Programming language will be written and executed is known as the syntax (by both the runtime system of the computer and by human developers). Perl, C, and Java are all closely related to Python in syntax. There are some definite distinctions between the languages, though. High readability was a priority when creating Python.

In this article, let us try to understand the multiple uses of the ‘@’ (at) symbol in Python along with its implementation.

The two major use of the “@” symbol in Python language are as follow:

  • As decorator
  • For matrix multiplication (as a binary operator)

Before learning the use of “@” as a decorator, let’s first understand what is a decorator and what is it used for.

What is a decorator?

A function called a decorator accepts another function as input, modifies it by adding some features, and then returns the updated function. Without changing the original function’s source code, one can do this.

One can easily extend the functionality of the current functions by decorating them. By including that code inside the wrapper function, we may do this. Therefore, one can simply add or change any code they want inside the wrapper function without changing the original method in any way.

To learn more about decorators click here.

Example of decorator without using “@”

#defining function with the wrapper function
def func1(func):
  def wrapper():
    print("Begin")
    func()
    print("End")

  return wrapper

#defining the function that is passed as parameter
def func2():
  print("AskPython")

#calling the function
x = func1(func2)
x()

Output

Decorator Example Without Using @ Symbol
Decorator Example Without Using @ Symbol

Use of ‘@’ for decorating in Python

Now that we have an idea of what a decorator is, let’s understand the use of the ‘@’ symbol in decorating.

Take a look at the above example, we have assigned a variable x which is equal to the first function (func1) with the second function (func2) passed as its parameter.

Then we call the variable to run the functions in the required way. Instead of working this way, the easy way is to add a line of code with the”@” symbol followed by name of the first function to it before starting with the second function. This works the same way as the above function. The benefit of using the “@” symbol method is that it makes code more readable and easier to understand.

The example below will give a clear idea about the same.

#defining function with the wrapper function
def func1(func):
  def wrapper():
    print("Begin")
    func()
    print("End")

  return wrapper

#defining the function that is passed as parameter
@func1
def func2():
  print("AskPython")

#calling the function
func2()

Output

Decorator Example Using @ Symbol
Decorator Example Using @ Symbol

There are several decorators users will encounter, but the most prevalent ones include

  • @property: It is possible to access a method like a regular attribute by tagging it with @property decorator.
  • @classmethod: When you require a method that includes the class but isn’t instance-specific, a class method can be helpful.
  • @staticmethod: A static method is associated with the class rather than the class instance.  The main distinction is that a static method doesn’t change the class in any way.

Use of ‘@’ for matrix multiplication

In Python 3.5, the @ operator can be overloaded.  It is termed __matmul__ since it is intended to perform matrix multiplication. To learn more about the same click here.

Below is the simple implementation of this method in Python programming language.

class example(list):
  def __matmul__(self, y):
    x = self
    return ([[sum(x[i][k] * y[k][j] for k in range(len(y)))
            for j in range(len(y[0])) ] for i in range(len(x))])

x = example([[1, 5],[2, 4]])
y = example([[3, 3],[4, 2]])

print("OUTPUT: ")
print(x @ y)

Output

Matrix Multiplication Using @ symbol
Matrix Multiplication Using @ symbol

Summary

In this article, we tried to comprehend the different uses of the “@” symbol. along with this, we also understood decorator, its uses, and benefits in the python programming language. To learn from more such easy to understand and detailed articles on various topics related to the Python Programming Language, check our courses section.