Skip to main content

A convenient way to create parametrized decorators.

Project description

Paramdec is a convenient way to create parametrized decorators.

Usage

Create your parametrized decorator:

>>> from paramdec import paramdec
>>> @paramdec
... def my_dec(func, foo=42, bar=None):
...     def wrapper(*func_args, **func_kwargs):
...         # Process foo and bar
...         return func(*func_args, **func_kwargs)
...     return wrapper

Use it with parameters:

>>> @my_dec(bar="bar")
>>> def func(): pass

Or without parameters:

>>> @my_dec()
>>> def func(): pass

Or even without parentheses:

>>> @my_dec
>>> def func(): pass

Also, consider using functools.wraps for your decorators.

Limitations

  • No positional args supported.

The rationale behind this is the fact that I have not found any pythonic way to make a decorator differentiate between a function to be decorated and a callback. So if you pass a single callable positional argument to a parametrized decorator, it will mistakenly decide that it was called not as a factory but as a casual decorator.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

paramdec-2.1.0-py2.py3-none-any.whl (4.9 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page