36

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?

Community
  • 1
  • 1
mstringer
  • 2,242
  • 3
  • 25
  • 36

2 Answers2

28

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
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • 5
    Eh, if you have a single-argument function named `decorator` then it's pretty clear what its argument is no matter what it's called. – Ben Jun 09 '12 at 00:59
  • 1
    Calling the inner function `decorator` is probably a very good idea, since `decorator_name` is not really a decorator but a *decorator factory*, and the multiple layers of nested function definition can be a little confusing. – Ben Jun 09 '12 at 01:01
  • 1
    Why abbreviate `function`? I asked this [here](http://stackoverflow.com/questions/10957789/using-the-name-function-for-a-variable-in-python-code), too. – mstringer Jun 09 '12 at 01:56
  • 3
    @MikeStringer I find myself using f or _f or g because it conforms to math notation (e.g. `f(x)`) and emphasizes that the function being passed doesn't need to be restricted in and of itself. (as opposed to a key function or comparison function, or a function that only takes one argument, etc). – Jeff Tratner Jun 09 '12 at 02:02
  • Where are `args` and `kwargs` defined in this example? – David Parks Apr 30 '22 at 00:40
  • can you explain why you use 3 nested methods instead of 2? Even the [functools.wraps(f) docs](https://docs.python.org/3/library/functools.html#functools.wraps) use only two. – David May 10 '23 at 08:52
  • 1
    @David because the decorator accepts arguments, which means that in `@foo(...)` the `foo` function actually needs to return the decorator that is them applied to the function (foo gets called at import time and then the return value of foo decorates your function) – ThiefMaster May 25 '23 at 08:22
2

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

rachid el kedmiri
  • 2,376
  • 2
  • 18
  • 40