Revision 3 as of 2021-03-27 11:41:36

Clear message

A page for useful (or potentially abusive?) decorator ideas.

Category

Decorator

Summary

builtin

@property

Turn function into propery accessor, also: @property.setter

builtin

@classsmethod

Use cls instead of self

builtin

@staticmethod

Defines methods without self handle

stdlib

@functools.wraps

Retain modicum of meta infos (__doc__) of wrapped function

stdlib

@functools.singledispatch

Allows for divergent implementations per @f.register()

stdlib/backports

@functools.lru_cache

Wrap computationally expensive functions

stdlib

@functools.cmp_to_key

Transform an old-style comparison function to a key function.

stdlib

@functools.total_ordering

Adds any of the missing lt,le,eq,ge,gt comparison dunder methods

stdlib

@functools.partial

Prepopulate a couple of function arguments

stdlib

@contextlib.contextmanager

Uses generator protocol yield to turn function into with-able context mananger

stdlib

@dataclasses.dataclass

Adds various “dunder” methods to the class to streamline property access

stdlib

@atexit.register

Run as shutdown function

stdlib

@abc.ABCMeta.register

Turn into abstract base class

stdlib

@enum.unique

Guarantee unique members in enum class

stdlib

@xmlrpc.….register_function

...

asyncio

@coroutine

Outdated scheme for async def, deprecated in 3.8

asyncio

@throttle(2,1)

Reduce invocations per timeframe

asyncio

@exceptionCatcher…

Log exceptions more instantly

asyncio

@timeit

Basic benchmarking

async

@async_process

make func async and execute in other process

testing

@unittest.mock.patch

Override function w/ signature

testing

@wrapt.patch_function_wrapper

Override function w/ signature

decorator

@decorator

Assists declaring signature-preserving and single dispatch decorators

decorator

@decorator_args

Ease parameterizing decorators themselvess

decorator

@named_decorator

Retain callees' function name to simplify tracebacks

decorator

@dec.decorator(before=cb,after=cb)

Craft trivial enter/exit decorator

collect

@click.option("-d")

Argparse alternative

collect

@config_decorator.setting

Declarative dict structure for application settings

collect

@regex_decorator.listen

Register function callbacks for pattern matching

typing

@contract({"param":gt(3)})

Exhaustive function parameter constraints

flow handling

@__main__

Automatically run a function if invocation script

flow handling

@retry(times=2)

Reinvokes function a few times when encountering (temporary) exceptions, alt: pypi:retry-decorator

monkeypatching

@inject(modules…)

Shorthand assignment to modules/objects

monkeypatching

@monkeybiz.patch(fn)

Swap out function in object, retain reference to original

(Note: Not sure this is going anywhere. Relisting builtins is somewhat redundant, but topical. The focus is novel/interesting decorators in the wild. The categorization probably won't hold up.)

__main__

This decorator does not alter a function, but causes it to be executed if __name__ == '__main__'. This provides an experimental cleaner syntax to the traditional way of bootstrapping a python script. This should not be used on class methods.

The function gets a copied list of sys.argv arguments.

Toggle line numbers
   1 def __main__(func):
   2 
   3     if __name__ == "__main__":
   4         import sys, os
   5         args = sys.argv[:]
   6         args[0] = os.path.abspath(args[0])
   7         func(args)

Unable to edit the page? See the FrontPage for instructions.