Skip to content

Decorators #76

Closed
Closed
@weepy

Description

@weepy

Was looking at Python for ideas to plunder. Not sure about the usefulness of them, but they looked interesting, and as they are syntactic sugar, they might be relevant. Here's the relevant section in Wikipedia:

A decorator is a Python object that can be called with a single argument, and that modifies functions or methods. Python decorators were inspired in part by Java annotations, and have a similar syntax; the decorator syntax is pure syntactic sugar, using @ as the keyword:

@viking_chorus
def menu_item():
    print "spam"
is equivalent to
def menu_item():
    print "spam"
menu_item = viking_chorus(menu_item)
Decorators are a form of metaprogramming; they enhance the action of the function or method they decorate. For example, in the above sample, viking_chorus might cause menu_item to be run 8 times for each time it is called:
def viking_chorus(myfunc):
    def inner_func(*args, **kwargs):
        for i in range(8):
            myfunc(*args, **kwargs)
    return inner_func

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions