Helps function wrappers/decorators with perfect forwarding
Project description
Simple helper for function wrappers or decorators that makes the wrapper function look indistinguishable from the original.
Installation
Using pip:
pip install funcwrap
Alternatively, you can redistribute the funcwrap.py module or even the funcwrap.wrap function by itself as part of your code or program without any license ramifications.
Usage
funcwrap.wrap is similar to the standard functools.wraps function. However, it returns a wrapper that imitates the wrapped function’s signature exactly on the python syntax level.
>>> from funcwrap import wraps
>>> def func(a='Hello,', *, b='World!'):
... pass
>>> @wraps(func)
... def wrapper(*args, **kwargs):
... return (args, kwargs)
>>> wrapper()
(('Hello,',), {'b': 'World!'})
>>> wrapper('Bye,', b='Quentin!')
(('Bye,',), {'b': 'Quentin!'})
IMPORTANT: If you’re planning to wrap callables other than python functions or lambdas (e.g. partials, methods, objects), be advised that the results may be surprising. Make absolutely sure that the wrapper function behaves as expected before using funcwrap!
Why (not) use decorator?
funcwrap is a lighter alternative to the decorator module. There are many reasons to stick with decorator and some for trying funcwrap.
Reasons to stick with decorator:
more well tested and empirically proven, mature package
has a different API that is more directly designed toward writing decorators
supports generator and coroutine functions
supports python versions below 3.5
and probably many more
Reasons to use funcwrap:
support for python 3.8’s positional-only parameters
simpler, shorter code that is easier to understand and modify if you need to
license: you can redistribute this module as part of your code or program without having to retain any license notice
has a different API that fits your needs better (@wraps(func) vs @decorator)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file funcwrap-0.0.1.tar.gz
.
File metadata
- Download URL: funcwrap-0.0.1.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 644e818e18d9189d560ae5dac0e11a40ced7da3fe938281215d683397c9745ab |
|
MD5 | 53557f682e2a8b5214a2f94519d1e4bf |
|
BLAKE2b-256 | 07cb23eaf2aab8ea5c16c294223ced0b4d9b69b90e24753fc410c0c8cd640ca9 |
File details
Details for the file funcwrap-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: funcwrap-0.0.1-py3-none-any.whl
- Upload date:
- Size: 3.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 311fb906965da7f5ff09ada1745458e57819977d3f2625b1bd508675811cf041 |
|
MD5 | 145d6f75ef85ca8017d41711c20001a9 |
|
BLAKE2b-256 | 0942ecc76e444af2d0351d488fb1e5b23527f2bd47723563a2f6cafa200e735e |