Python naming conventions in decorators

Question:

Are there any “accepted” naming conventions for the innards of Python decorators?

The style guide doesn’t mention it, and this awesome entry about decorators is pretty consistent in using variants of “wrapped” for the ultimate function that is returned, but what about the names used when creating decorators that take arguments?

def decorator_name(whatevs):
    def inner(function):
        def wrapped(*args, **kwargs):
            # sweet decorator goodness
        return wrapped
    return inner

Specifically, what are the conventions for inner, function, and wrapped in the above example?

Asked By: mstringer

||

Answers:

There are no standardized conventions (such as PEPs) for those names. If you check the python stdlib you’ll find lots of different names for those functions.

However, decorator is a rather common name for the decorator function inner.
It is also common to call your wrapped function wrapper and decorate it with functools.wraps(f) with f being the wrapped function (func is also a common name for it).

def decorator_name(whatevs):
    def decorator(f):
        @wraps(f)
        def wrapper(*args, **kwargs):
            pass # sweet decorator goodness
        return wrapper
    return decorator
Answered By: ThiefMaster

By definition, a decorator in Python could be a function or a class, so the type of implementation (class or function), is the factor deciding which naming convention to use.
a common mistake is to prefix the name of the function with the keyword decorator.
for more information about decorators see link

Answered By: rachid el kedmiri
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.