Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Pie Syntax (@) in Python
The @ symbol in Python is called decorator syntax and is used to modify the behavior of functions, methods, or classes. Decorators allow you to wrap another function or class with additional functionality without permanently modifying their code.
Syntax
@decorator_name
def function_name():
# function body
Using Function Decorators
Basic Decorator Example
A decorator function takes another function as an argument and extends its behavior ?
def my_decorator(func):
def wrapper():
print("Before function execution")
func()
print("After function execution")
return wrapper
@my_decorator
def say_hello():
print("Hello World!")
say_hello()
Before function execution Hello World! After function execution
Multiple Decorators
You can apply multiple decorators to a single function. They are applied from bottom to top ?
def decorator_one(func):
def wrapper():
print("Decorator One - Start")
func()
print("Decorator One - End")
return wrapper
def decorator_two(func):
def wrapper():
print("Decorator Two - Start")
func()
print("Decorator Two - End")
return wrapper
@decorator_one
@decorator_two
def greet():
print("Hello from decorated function!")
greet()
Decorator One - Start Decorator Two - Start Hello from decorated function! Decorator Two - End Decorator One - End
Using Class Method Decorators
@classmethod Decorator
The @classmethod decorator creates methods that receive the class as the first argument instead of the instance ?
class Student:
count = 0
def __init__(self, name):
self.name = name
Student.count += 1
@classmethod
def get_student_count(cls):
return cls.count
# Create student instances
student1 = Student("Alice")
student2 = Student("Bob")
student3 = Student("Charlie")
print(f"Total students: {Student.get_student_count()}")
Total students: 3
@staticmethod Decorator
The @staticmethod decorator creates methods that don't receive any automatic arguments ?
class MathUtils:
@staticmethod
def add_numbers(a, b):
return a + b
@staticmethod
def multiply_numbers(a, b):
return a * b
# Use static methods without creating an instance
result1 = MathUtils.add_numbers(5, 3)
result2 = MathUtils.multiply_numbers(4, 7)
print(f"Addition: {result1}")
print(f"Multiplication: {result2}")
Addition: 8 Multiplication: 28
Common Built-in Decorators
| Decorator | Purpose | Usage |
|---|---|---|
@classmethod |
Creates class methods | Access class variables |
@staticmethod |
Creates static methods | Utility functions in classes |
@property |
Creates getter methods | Access methods like attributes |
Conclusion
The @ symbol in Python provides a clean syntax for applying decorators to functions and methods. Decorators are powerful tools for extending functionality, implementing cross-cutting concerns like logging, and creating cleaner, more maintainable code.
