Ask Documentation
WebsiteGitHubPyPI
  • Introduction
  • Getting Started
    • Install & Get Started
    • Hello, World!
  • Routes & Requests
    • Routes
    • Defining Routes
    • Request Data
    • HTTP Status Codes & Methods
    • CORS
  • Response
    • JSON Response
    • JSON Response With an HTTP Status Code
  • Classes
    • Class Instance Variable
    • Initialization/Constructor Method
  • Data Types
    • Dictionaries
  • Built-in Utilities
    • Quick_set()
    • Deep()
    • Serialize()
    • Require_keys()
    • Random Generators
    • Pattern Matching
    • Email
  • Database
    • Ask and Databases
    • Models/Classes
      • Columns
      • Initialization/Constructor
      • Serialization
      • The &basic decorator
    • CRUD
      • Add
      • Select
      • Update
      • Delete
    • Check if a Row Exists
    • Sorting
    • Database Lists
  • JWT Authentication
    • Introduction
    • Protecting Routes
    • How to Create a Basic Login System
    • Properties & Methods of _auth
    • Making Requests to Protected Routes
  • Decorators
    • What are Decorators?
    • Create and Use Custom Decorators
    • Built-in Decorators
  • Security
    • Hashing
    • Route Security
    • Environment Variables
  • Configuring the Transpiler
  • Askfile.toml
  • Modules & Libraries
    • Importing an Ask Module
    • Includes
    • Importing Python Modules
  • Development Tools
    • Editor Syntax Highlighting
    • Automatic API documentation
    • CLI Flags
    • Running in development mode
    • Versioning System
  • Contribute
    • Feature Requests
    • Bug Reports
    • Contribute Code
Powered by GitBook
On this page
Export as PDF
  1. Decorators

Create and Use Custom Decorators

How to create and use your own custom decorators.

PreviousWhat are Decorators?NextBuilt-in Decorators

Last updated 4 years ago

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

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.

Example

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:

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".

example