What does the at symbol do in Python?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
The "at" (@) symbol in Python is known as the "at sign" and serves several purposes depending on the context in which it is used. This article covers the various functionalities and use cases of the "at" symbol in Python, including decorators, matrix multiplication, and its role in special contexts within the language.
Decorators
What are Decorators?
In Python, decorators are a way to modify or enhance the behavior of a function or class method. The "at" symbol is used to apply decorators to functions or methods. A decorator is essentially a function that takes another function and extends its behavior without explicitly modifying it.
Example of a Function Decorator
In this example, say_hello is decorated with log_execution, which logs the execution of the function.
Class Method Decorators
Class methods can also be decorated using the "at" symbol:
In this case, my_static_method and my_class_method are static and class methods, respectively, distinguished by the use of decorators.
Matrix Multiplication
With the introduction of PEP 465 in Python 3.5, the "at" symbol @ was adopted as an infix operator for matrix multiplication. It provides a clean and clear syntax for performing matrix operations, especially useful in scientific computing and data analysis.
Example of Matrix Multiplication
Here, A @ B multiplies matrices A and B, resulting in a new matrix C.
Summary Table
Here's a quick summary of the different contexts in which the "at" symbol is used in Python:
| Context | Usage | Description |
| Decorators | @decorator | Enhances or modifies the behavior of a function or method. |
| Static Methods | @staticmethod | Defines a static method in a class. |
| Class Methods | @classmethod | Defines a class method that receives the class as its argument. |
| Matrix Multiplication | A @ B | Performs matrix multiplication between matrices A and B. |
Additional Details
Chained Decorators
Python allows multiple decorators to be applied to a single function. They are applied from top to bottom, wrapping the function in multiple layers. Here's an example of chained decorators:
In this example, greet is first decorated with uppercase, which transforms its output to uppercase, and then with log_execution to log its execution.
Custom Decorators
You can create custom decorators to do nearly anything. Based on the requirement, decorators can validate inputs, cache results, enforce access control, etc.
Conclusion
The "at" (@) symbol in Python is a versatile tool that plays a crucial role in enhancing the language's functionality. Whether it's decorating functions for more complex behavior, utilizing efficient matrix operations in numerical computing, or enabling cleaner syntax through scoped functionality, this symbol is a powerful feature in Python that promotes clean, readable, and efficient code.
Related reading
- What does the 'b' character do in front of a string literal?
- What does the 'b' character do in front of a string literal?
- What does the Ellipsis object do?
- What does the Ellipsis object do?
- What does the fit method in scikit-learn do?
- What does the .numpy function do?
- What does the slash mean when help is listing method signatures?
- What does the use_multiprocessing input argument in keras mode.fit do?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.