Python Decorators Example W3Schools Tutorial Guide

Logan Jul 13, 2026

Python decorators are a powerful feature that allows us to modify existing functions or methods and add new functionality. They are incredibly useful and enable us to abstract common behavior and modularize code. Let's explore decorators in Python with some practical examples, guided by the renowned teaching platform w3schools.

How to Use Decorators in Python, by example | Towards Data Science
How to Use Decorators in Python, by example | Towards Data Science

Before delving into decorators, let's quickly recap functions in Python. A function in Python is defined using the `def` keyword, followed by the function name and parentheses. Brackets then contain the function body. Here's a simple example:

Python Decorators with Examples
Python Decorators with Examples

Understanding Decorators

Decorators in Python use the `@` symbol followed by the decorator function's name. They "wrap" or modify the function it precedes. The basic syntax for a decorator is a function that takes in a function and returns another function.

Python Decorators - The advanced concept in functional programming
Python Decorators - The advanced concept in functional programming

w3schools provides a great introduction to decorators, starting with a simple example. Let's see how to create a decorator function that greets users:

Simple Decorator Example

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

The following code demonstrates a simple decorator that adds a greeting to functions:

```python def greet(f): def wrapper(*args, **kwargs): print("Hello, before the function!") f(*args, **kwargs) print("Hello, after the function!") return wrapper ```

Here, `greet` is our decorator function that wraps the input function `f`. It prints a greeting before and after the function call.

To use this decorator, we prefix our function with `@greet`, like so:

Uses of Decorators in python
Uses of Decorators in python

```python @greet def say_hi(name): print(f"Hi, {name}!") say_hi("John") ```

Decorating Functions with Arguments

Decorators can also take arguments. Let's see how we can create a dynamic greeting decorator:

```python def greet(greeting): def greet_func(f): def wrapper(*args, **kwargs): print(f"{greeting}, before the function!") f(*args, **kwargs) print(f"{greeting}, after the function!") return wrapper return greet_func @greet("Howdy") def say_howdy(name): print(f"Howdy, {name}!") say_howdy("Jane") ```

In this example, the `greet` function takes a `greeting` argument and returns another function `greet_func`, which is our actual decorator. This allows us to create dynamic decorators that can be customized at runtime.

The Simplest Tutorial for Python Decorator | Towards Data Science
The Simplest Tutorial for Python Decorator | Towards Data Science

Decorating Functions with Arguments and Return Values

Decorators can also handle functions that return values. Let's see how we can modify our previous example to include a return statement:

| Pythonista Planet
| Pythonista Planet
Decorating Your Python Code: A Beginner’s Guide to Decorators
Decorating Your Python Code: A Beginner’s Guide to Decorators
tuple in python
tuple in python
7 Cool Python Projects for Kids to Try!
7 Cool Python Projects for Kids to Try!
a screenshot of a computer screen with an arrow pointing to the text, which is highlighted in green
a screenshot of a computer screen with an arrow pointing to the text, which is highlighted in green
50 Python Project Ideas for Diploma Students
50 Python Project Ideas for Diploma Students
a computer screen with an image of a flower in the center and text that reads, colorful flowers in python
a computer screen with an image of a flower in the center and text that reads, colorful flowers in python
the top 60 python project ideas
the top 60 python project ideas
a computer screen with an image of a starburst in the background and text on it
a computer screen with an image of a starburst in the background and text on it
the top 5 python project ideas
the top 5 python project ideas
OOP in Python
OOP in Python
Code in Python – Devs' Sticker | Snake-Logo Python Vision Board, Python Logo Png, Python Logo Design, Python Sticker, Python Programming Text With Logo, Python Programming Training Logo, Python And Machine Learning Logos, Python Coding Symbol Design, Python Code With Snake Logo
Code in Python – Devs' Sticker | Snake-Logo Python Vision Board, Python Logo Png, Python Logo Design, Python Sticker, Python Programming Text With Logo, Python Programming Training Logo, Python And Machine Learning Logos, Python Coding Symbol Design, Python Code With Snake Logo
Python OOP Tutorial 6: Property Decorators - Getters, Setters, and Deleters
Python OOP Tutorial 6: Property Decorators - Getters, Setters, and Deleters
python
python
25 Python Project Ideas for Beginners (With Tech Stack)
25 Python Project Ideas for Beginners (With Tech Stack)
wallpaper_python
wallpaper_python
Emojis in python
Emojis in python
an advertisement for the company's website, which is designed to look like it has been
an advertisement for the company's website, which is designed to look like it has been
💜 Python Trick: Use f-Strings 🐍 Write cleaner and more readable Python code with f-strings ✨
💜 Python Trick: Use f-Strings 🐍 Write cleaner and more readable Python code with f-strings ✨

Decorating Functions with Return Values

To include return values, we must return the result of the wrapped function's call in the wrapper:

```python def greet(greeting): def greet_func(f): def wrapper(*args, **kwargs): print(f"{greeting}, before the function!") result = f(*args, **kwargs) print(f"{greeting}, after the function!") return result return wrapper return greet_func ```

Using this updated decorator, our call to `say_howdy` will now return a value:

```python say_howdy("Jane") ```

Decorators in Python are a powerful tool that can significantly improve code readability and modularity. With them, you can abstract common behavior out of your functions, promoting code reuse and improving maintainability.

If you want to dive deeper into decorators, consider exploring advanced use cases such as class decorators or using decorators with generics. PEP-0318 is an excellent resource for learning about decorators in Python. Now that you have a solid understanding of decorators, go ahead and experiment with this new, powerful tool in your programming toolbox!