Python Decorators for Beginners: Master the Basics Fast

Logan Jul 13, 2026

Ever found yourself wishing you could modify a function's behavior, without actually changing the function itself? This is where Python decorators come into play! Decorators are a powerful feature that allows you to easily modify the behavior of a function, class, or method, or even create a shorthand for common configurations. But don't let the name intimidate you; they're not as scary as they sound. Let's demystify Python decorators and learn how to use them, with an emphasis on beginner-friendly examples.

Coding For Beginners Python - Decorators
Coding For Beginners Python - Decorators

Python decorators exploit the fact that functions are first-class objects in Python. This means you can treat functions as variables, store them in data structures, or even pass them as arguments. Understanding this concept is key to grasping decorators, as they essentially 'decorate' or wrap around other functions, modifying their behavior.

Coding For Beginners Python - Decorators
Coding For Beginners Python - Decorators

Decorators in Action: A Simple Example

Let's start with a simple decorator demonstration. Suppose we want to print a message before and after every function call. We can create a decorator to encapsulate this functionality:

How Python Decorators Work: 7 Things You Must Know
How Python Decorators Work: 7 Things You Must Know

```python def my_decorator(func): def wrapper(): print("Something is about to happen.") func() print("Something just happened.") return wrapper def say_hello(): print("Hello, world!") ```

Here, my_decorator is the decorator function that takes another function as an argument (func). Inside my_decorator, we define a new function wrapper that contains the logic we want to apply - in this case, printing messages before and after calling func. Finally, we return wrapper.

Applying the Decorator

| Pythonista Planet
| Pythonista Planet

Applying the decorator to say_hello is as simple as putting the function name followed by the decorator in parentheses:

```python @my_decorator def say_hello(): print("Hello, world!") ```

This is equivalent to:

```python say_hello = my_decorator(say_hello) ```

Using the decorator syntax @my_decorator makes the code cleaner and more readable.

10 Python Mini Projects for Beginners | Easy Coding Ideas for Students & Resume
10 Python Mini Projects for Beginners | Easy Coding Ideas for Students & Resume

Using Decorators with Arguments

Decorators can also take arguments. Let's modify the previous example to allow customizing the print message:

```python def my_decorator(some_message): def wrapper(func): print(some_message) func() print(some_message) return wrapper @my_decorator("This is a message...") def say_hello(): print("Hello, world!") ```

In this case, my_decorator takes an argument some_message and uses it to print a custom message. The resulting decorated function can then be used like any other function.

Decorating Your Python Code: A Beginner’s Guide to Decorators
Decorating Your Python Code: A Beginner’s Guide to Decorators

Built-in Decorators: @staticmethod and @classmethod

Python comes with several built-in decorators. Two of the most common are @staticmethod and @classmethod, used to define class methods and static methods:

Python Decorators with Examples
Python Decorators with Examples
The Simplest Tutorial for Python Decorator | Towards Data Science
The Simplest Tutorial for Python Decorator | Towards Data Science
Python Decorators Explained – Functions, Syntax, and Real Examples
Python Decorators Explained – Functions, Syntax, and Real Examples
an image of a computer screen with the text, 1 basic decorator on it
an image of a computer screen with the text, 1 basic decorator on it
Python Projects Ideas For Beginners
Python Projects Ideas For Beginners
5 Python Projects Every Beginner Must Build 💻
5 Python Projects Every Beginner Must Build 💻
Python Decorators - The advanced concept in functional programming
Python Decorators - The advanced concept in functional programming
Ball Python Enclosure Setup Guide: Create the Perfect Habitat
Ball Python Enclosure Setup Guide: Create the Perfect Habitat
9 Python Projects for Beginners to Build in 2026 | Portfolio-Worthy Python Ideas
9 Python Projects for Beginners to Build in 2026 | Portfolio-Worthy Python Ideas
30 Python Projects For Beginners and Advanced Learners (2023)
30 Python Projects For Beginners and Advanced Learners (2023)
5 Python Projects for Beginners
5 Python Projects for Beginners
Learn Python Roadmap 2026 | Beginner to Expert Python Coding & Data Science
Learn Python Roadmap 2026 | Beginner to Expert Python Coding & Data Science
Python OOP Tutorial 6: Property Decorators - Getters, Setters, and Deleters
Python OOP Tutorial 6: Property Decorators - Getters, Setters, and Deleters
From Authentication to Caching: Real-World Applications of Python Decorators
From Authentication to Caching: Real-World Applications of Python Decorators
dev
dev
How to Use Decorators in Python, by example | Towards Data Science
How to Use Decorators in Python, by example | Towards Data Science

```python class MyClass: @staticmethod def my_static_method(): print("This is a static method.") @classmethod def my_class_method(cls): print("This is a class method.", cls) ```

Static methods belong to classes, but they don't have access to instances or the class they belong to. Class methods, on the other hand, have access to the class, but not to instances. They are commonly used for alternative constructors.

Visibility of Decorated Methods

Decorators don't change the visibility of functions. In the above example, my_static_method and my_class_method are still instance methods, meaning they can be called as such:

```python my_object = MyClass() my_object.my_static_method() my_object.my_class_method() ```

However, using @staticmethod and @classmethod makes your code clearer and more Pythonic. It communicates your intent more effectively by stating whether a method is meant to be accessed via instances or the class itself.

Decorators in Practice: Logging

Let's use decorators to create a simple logging mechanism. We'll make a decorator that prints a timestamp and the name of the function being called, along with its arguments:

```python import functools import time def my_logger(func): @functools.wraps(func) def wrapper(*args, **kwargs): print(f"{time.strftime('%Y-%m-%d %H:%M:%S')} - Calling {func.__name__} with args {args} and kwargs {kwargs}") return func(*args, **kwargs) return wrapper ```

To use this decorator, simply place it above any function you want to log:

```python @my_logger def add(x, y): return x + y ```

The @functools.wraps decorator is used to preserve the original function's metadata (like its name and docstring), which would otherwise be lost when we replace the function with our wrapper.

Congratulations! You've just learned the basics of Python decorators and seen how they can be applied to make your code more readable, functional, and maintainable. Like any tool, decorators have their place - use them when they make your code clearer or more concise, and remember that overusing decorators can make your code harder to understand. Happy decorating!