> For the complete documentation index, see [llms.txt](https://docs.ask.edvard.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ask.edvard.dev/decorators/create-and-use-custom-decorators.md).

# Create and Use Custom Decorators

How to create and use your own custom decorators.

A decorator uses another concept of programming called first-class functions. Essentially it allows you to treat a function as a variable by passing it to another function.

The structure of a decorator is pretty simple. The decorator is given a name and you can then choose to either do some actions before executing the inner function (the one that gets passed in) or after, or both. It's easier to explain by [example](/decorators/create-and-use-custom-decorators.md#example)

A decorator is defined with the keyword `decorator` followed by the name of the decorator. After that it works like a normal function except fo one thing. You can call `inner()` somewhere in the decorator body, this is where the "passed in" function (the one the decorator is used on) will get executed. You can put any code before or after this function, you can also execute `inner()` multiple times or e.g. in a loop.

Then put `&` and the name of a decorator to decorate any function.&#x20;

## Example

```python
decorator my_first_decorator:
  print('Before')
  inner()
  print('After')

&my_first_decorator
def my_function(message):
  print(message)

my_function('Hello')
```

The output of this program would be:

```bash
Before
Hello
After
```

As you can see what happened is that the function `my_function` got passed into `my_first_decorator`, which then first printed "Before" then ran the "inner" function, and then printed "After".


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.ask.edvard.dev/decorators/create-and-use-custom-decorators.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
