Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Statement decorators

Tags:

People also ask

What are decorators in programming?

Decorators are functions (or classes) that provide enhanced functionality to the original function (or class) without the programmer having to modify their structure.

What are function decorators?

By definition, a decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it. This sounds confusing, but it's really not, especially after you've seen a few examples of how decorators work.

What is decorator explain with example?

A decorator is a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure. Decorators are usually called before the definition of a function you want to decorate.

What is the use of decorators in Python?

Decorators are one of the most helpful and powerful tools of Python. These are used to modify the behavior of the function. Decorators provide the flexibility to wrap another function to expand the working of wrapped function, without permanently modifying it.


We have some code that looks like this:

from third_party_library import foo

for n in range(3):
    try:
        foo(args)
        break
    except:
        print "Retry %i / 3" % n

I would like to use a decorator, allowing our code to be more consise, looking like this:

from third_party_library import foo

@retry(3)
foo(args)

This gives a syntax error. Am I missing something, or does python just not allow decorators on statements?