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, commonly known as the decorator symbol, is a powerful feature that allows developers to modify the behavior of functions or classes. The @ symbol is placed before a function, indicating that a decorator is being applied. Decorators have the ability to run additional code before and after the function they decorate, without modifying the function itself. This enables more modular and expressive code.
What is a Decorator?
In Python, decorators are essentially functions that add functionality to an existing function or method, without changing its structure. The typical structure of a decorator involves a higher-order function, a function that takes one or more functions as inputs and returns a new function.
Basic Example of a Decorator
Here is a simple example of a decorator that logs the activity of a function:
When say_hello() is called, the output will be:
Using Decorators with Parameters
More complex decorators can also accept arguments. To do this, you need another level of function nesting:
This will output "Hello Alice" three times.
Decorators in Python Standard Library
Python's standard library comes with several built-in decorators, like @staticmethod, @classmethod, and @property.
@staticmethodis used to declare a method as a static method, which means it doesn't operate on an instance of the class nor modify the class state.@classmethodmodifies a method to pass the class itself as the first argument instead of an instance of the class.@propertyallows a method to be accessed like an attribute instead of as a method with parentheses.
Summary in a Table
| Decorator | Description | Typical Use Case |
@staticmethod | Converts a method into a static method, without class scope. | Methods not modifying class instance. |
@classmethod | A method that receives the class as implicit first argument. | Factory methods. |
@property | Makes a method accessible as an attribute. | Simplifies access to calculations. |
Advanced Decorator Concepts
Decorators can also be used to manage authentication, enforce permissions, cache results, and log operations in a uniform way across multiple functions or classes.
Conclusion
The @ symbol in Python is not only syntactically elegant but also extremely powerful, providing developers with a flexible tool to adhere to the DRY (Don't Repeat Yourself) principle and extend functionality without modifying existing code. It is a hallmark of Python's support for writing clean, efficient, and reusable code.
Related reading
- What does the at symbol do in Python?
- 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?
.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.