Python partial on steroids
Like python functools.partial but with change/extend access to args and kwargs.
Also a great tool even if no partial is needed but the function takes args/kwargs that often depends on logic.
Easiest used as a decorator
from partialize import partialize
@partialize
def dummy(a, b, c=None):
return 'hello world'
partial_dummy = dummy.partial(1)
partial_dummy.b = 2
partial_dummy(c=3)
or invoked inline…
partial_dummy = partialize(dummy)
partial_dummy.a = 1
partial_dummy.update(b=2)
partial_dummy()
partial_dummy(c=3)
Use it on functions that needs logic to affect arguments passed, instead of building and passing a dict as kwargs which often gets quite messy and hard to read.
Dict kwargs example:
from partialize import partialize
@partialize
def search_products(site, query=None, brand=None, tags=None):
pass
kwargs = {}
if logic:
kwargs['query'] = q
if more_logic:
kwargs['brand'] = 'brand name'
products = search_products(site, **kwargs)
Partialize example:
search = search_products.partial(site)
if logic:
search.query = q
if more_logic:
search.brand = 'brand name'
products = search()
Note: function argument names are validated when set, unlike dict string keys.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
partialize-0.0.3.tar.gz
(4.7 kB
view details)
File details
Details for the file partialize-0.0.3.tar.gz.
File metadata
- Download URL: partialize-0.0.3.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
54a9038139987c7d073f6f929225c8ab533590cbeba77df7a3b0f629e03915bc
|
|
| MD5 |
6646bc997d6f1ddd4d9799f4ba398720
|
|
| BLAKE2b-256 |
26112352f88f9c17f3d49a6132afa1fadc2d7e1ea4c441ab0d7d642e26a5adee
|