Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python naming conventions in decorators

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?

like image 535
mstringer Avatar asked Jun 09 '12 00:06

mstringer


People also ask

What are Python naming conventions?

Rules for Python variables: A variable name must start with a letter or the underscore character. A variable name cannot start with a number. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )

What is the order of decorators in Python?

During the definition, decorators are evaluated from bottom to top meanwhile during the execution (which is the most important part in general) they are evaluated from top to bottom.

How should Python files be named?

Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.

What are different types of decorators in Python?

In fact, there are two types of decorators in Python — class decorators and function decorators — but I will focus on function decorators here. Before we get into the fun details of how a basic decorator works and how to implement your own decorators, let's see why we need them in the first place.


1 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 
like image 195
ThiefMaster Avatar answered Sep 27 '22 20:09

ThiefMaster