Understanding Python’s @: decorators, matrix ops, pandas

Introduction

When learning Python, you will often encounter the “@” (at sign) in code. Typical examples include decorators (@staticmethod etc.) and the @ operator used for matrix multiplication. However, for newcomers it can raise the question, “What does this symbol mean?” In this article, we will explain in detail the role and usage of “@” in Python. Specifically, we will introduce how to use it with decorators, matrix multiplication, pandas’ query() method, providing clear explanations for beginners.

What is “@” in Python?

In Python, “@” is primarily used as a “decorator”. A decorator is a mechanism for modifying the behavior of functions or methods. Since Python 3.5, the “@” operator has been introduced, allowing concise expression of matrix multiplication. Furthermore, in the pandas query() method, “@” can be used to reference variables. The usage of “@” is explained from the following three perspectives.
  1. Decorator: Adds specific functionality to functions or classes.
  2. Matrix multiplication (@ operator): Computes matrix products using NumPy, etc.
  3. pandas query() method: References variables within DataFrame queries.

What is a Decorator?

Basics of Decorators

A decorator is a mechanism that modifies a function’s behavior by placing an “@” before a function or method. It is mainly used for the following purposes.
  • Logging
  • Measuring execution time
  • Restricting function behavior (e.g., authentication)

Decorator Syntax

The basic syntax for using decorators in Python is as follows.
def my_decorator(func):
    def wrapper():
        print("Before executing the process")
        func()
        print("After executing the process")
    return wrapper

@my_decorator
def my_function():
    print("The main function has been executed")

my_function()

Decorator Output

Before executing the process
The main function has been executed
After executing the process

Practical Examples of Using Decorators

Decorators are also included extensively in the standard library. For example, the “@” is used when defining class methods and static methods.

@classmethod

Use @classmethod when defining a class method.
class MyClass:
    class_variable = "Class variable"

    @classmethod
    def class_method(cls):
        return f"Class method was called: {cls.class_variable}"

print(MyClass.class_method())

@staticmethod

Static methods use @staticmethod.
class MyClass:
    @staticmethod
    def static_method():
        return "Static method was called"

print(MyClass.static_method())

The “@” Operator in Matrix Operations

Python 3.5 and later introduced the @ operator. It is an operator for concisely expressing matrix multiplication.

Matrix Multiplication Using NumPy

import numpy as np

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])

C = A @ B
print(C)

Using ‘@’ in pandas’ query() method

pandas’ query() method allows you to use @ to reference variables.

Filtering using variables

import pandas as pd

df = pd.DataFrame({'name': ['Alice', 'Bob', 'Charlie'], 'age': [25, 30, 35]})
threshold = 30

# Refer to the value of threshold
filtered_df = df.query('age > @threshold')
print(filtered_df)

Summary

  • Decorators are used to modify the behavior of functions or classes.
  • The “@\” matrix multiplication operator makes computing matrix products concise in NumPy and similar libraries.
  • pandas’ query() method allows the use of “@” for variable references.
Understanding the uses of “@” and applying them appropriately enables more efficient Python programming.