Decorators (Slideshow)

The Ominous “@

  • Special syntax ⟶ only syntactic sugar

  • Used to wrap existing functions/methods

    • Without modifying them

  • Examples

    • Debugging: printing function name and arguments

    • Timing information: time spent inside a function

    • Adding a D-Bus interface to a plain Python class

    • Defining Flask URL routes to call Python functions

What and How

  • Only syntactic sugar for inner functions/closures (see Closures)

  • Can be implemented as functions

@debug
def bar(a, b, c):
    ...
  • Can be implemented as classes (usually when decorator is parameterized ⟶ state needs to be remembered)

@debug(prefix)
def bar(a, b, c):
    ...