Here Be Dragons

Lorem Ipsum

Melissa Nuño17 days ago

Lorem ipsum dolor

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce vitae lobortis orci. Donec euismod auctor sem vitae lacinia. Sed sem ex, bibendum a placerat in, dictum sed erat. Nunc suscipit molestie faucibus. Donec gravida neque id justo pulvinar semper laoreet nec nulla. Nunc tempor eros vitae dui vulputate, eget dictum mauris dignissim. Proin varius, urna et ultricies luctus, velit lacus tristique dui, non convallis turpis sem nec leo. Vivamus eu euismod lorem, sed fermentum…

Writing Python Decorators

Melissa Nuño9 years ago

So Easy, a Caveman Could Do It

A decorator is merely a function (or callable object) that accepts a function and returns a function. A very basic decorator can be defined like any other function:

def my_decorator(func):
    return func

Functionally, the following two snippets are the same:

@my_decorator
def my_func():
    pass
def my_func():
    pass
my_func = my_decorator(my_func)

Notice that the function call is implied in the first case. The portion after the @ character is evaluated when the function is defined, and just has to resolve to a callable object. There is no magic involved here.

The Ol’ Bait an’ Switch

As you…