Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Write Your First Decorator | Decorator Fundamentals
Python Decorators Explained

bookChallenge: Write Your First Decorator

You have already learned that a decorator in Python is a function that takes another function as its argument and returns a new function that can add extra behavior before or after the original function runs. Decorators are applied using the @decorator_name syntax directly above the function definition. This allows you to modify or enhance functions in a reusable way, without changing their actual code.

Opgave

Swipe to start coding

Create a decorator called print_status that wraps any function so that it prints "Function started" before calling the function and "Function ended" after the function finishes. The decorator must work with functions that accept any number of arguments.

Løsning

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 3
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Can you give me an example of a simple decorator?

What are some common use cases for decorators in Python?

How do I write my own custom decorator?

close

bookChallenge: Write Your First Decorator

Stryg for at vise menuen

You have already learned that a decorator in Python is a function that takes another function as its argument and returns a new function that can add extra behavior before or after the original function runs. Decorators are applied using the @decorator_name syntax directly above the function definition. This allows you to modify or enhance functions in a reusable way, without changing their actual code.

Opgave

Swipe to start coding

Create a decorator called print_status that wraps any function so that it prints "Function started" before calling the function and "Function ended" after the function finishes. The decorator must work with functions that accept any number of arguments.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 3
single

single

some-alt