Language

Advanced Python · Lesson 31 of 56

Decorators

Source: 9-Advance Python Concepts/9.3-Decorators.ipynb

Start here — no coding background needed

What you will learn

Wrap functions to add extra behavior — intermediate topic.

In simple words

Decorator modifies or logs a function without rewriting its core logic.

Power tools for later — iterators, generators, decorators. Skim if new; revisit anytime.

Easy example — try this first

Easy example — run this first. Change values and press Run again.

Python

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference notes (from full bootcamp)

Optional — deeper detail for when you are ready

Decorators

Decorators are a powerful and flexible feature in Python that allows you to modify the behavior of a function or class method. They are commonly used to add functionality to functions or methods without modifying their actual code. This lesson covers the basics of decorators, including how to create and use them.

Reference example
Python

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
'Welcome to the advanced python course'

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
Welcome to the advanced python course
Welcome to the advanced python course

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
Welcome to the advance python course
Welcome everyone
Please learn these concepts properly

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
Welcome to the advance python course
Welcome everyone to this tutorial
Please learn these concepts properly

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
Welcome to the advance python course
5
Please learn these concepts properly

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
6

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
This is an advanced python course

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
Welcome to the advance python course
This is an advanced python course
Please learn these concepts properly

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
Welcome to the advance python course
This is an advanced python course
Please learn these concepts properly

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
Something is happening before the function is called.
Hello!
Something is happening after the function is called.

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
Hello
Hello
Hello

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Conclusion

Decorators are a powerful tool in Python for extending and modifying the behavior of functions and methods. They provide a clean and readable way to add functionality such as logging, timing, access control, and more without changing the original code. Understanding and using decorators effectively can significantly enhance your Python programming skills.

Practice test — try yourself

Write code, press Check. Wrong answer shows the correct code to copy & run.

You learned "Decorators". Use print() to show: Done: Decorators

Hint: Use one print() with the exact text.

Python