Python naming conventions in decorators

后端 未结 2 1531
情歌与酒
情歌与酒 2021-02-05 04:37

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 prett

2条回答
  •  说谎
    说谎 (楼主)
    2021-02-05 04:57

    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
    

提交回复
热议问题