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 details)

Uploaded Python 2 Python 3

File details

Details for the file paramdec-2.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for paramdec-2.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 717a07551182ca16dc45d30b207dd8cd3706da9998ac3ec7f06ea81d77d34206
MD5 4178cb5b647ce39bedda6df9d59e4049
BLAKE2b-256 a98cc59390dfbb53f858d1b6b7217f74163b90c588f195b11121a61108120033

See more details on using hashes here.

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