Created
January 24, 2022 22:06
-
-
Save cgranade/c57865dd5d2412725943baa6b938d224 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"id": "increasing-senegal", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class partial_manager(object):\n", | |
" # NB: Named as a function instead of a class, since it gets used as though\n", | |
" # it was a function.\n", | |
" def __init__(self, fn, *args, **kwargs):\n", | |
" self._fn = fn\n", | |
" self._args = list(args)\n", | |
" self._kwargs = dict(kwargs)\n", | |
" \n", | |
" def __enter__(self):\n", | |
" return self\n", | |
" \n", | |
" def __exit__(self, exc_type, exc_val, exc_tb):\n", | |
" return self._fn(*self._args, **self._kwargs)\n", | |
" \n", | |
" def __call__(self, *values):\n", | |
" self._args += values\n", | |
" \n", | |
" def __getattribute__(self, attr_name):\n", | |
" if attr_name in ('_fn', '_args', '_kwargs'):\n", | |
" return super().__getattribute__(attr_name)\n", | |
"\n", | |
" def accumulate_kwarg(val):\n", | |
" self._kwargs[attr_name] = val\n", | |
" \n", | |
" return accumulate_kwarg" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "driving-lingerie", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def add(u, v, w, x, y, z, callback):\n", | |
" callback(u + v + w + x + y + z)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "focused-afghanistan", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"21\n" | |
] | |
} | |
], | |
"source": [ | |
"# Args and kwargs can both be passed when creating the partial.\n", | |
"with partial_manager(add, 2, 3, y=1) as a:\n", | |
" # Calling the partial afterwards allows for adding additional args and kwargs.\n", | |
" a(4, 5)\n", | |
"\n", | |
" # Kwargs can also be added by referring to them as attributes.\n", | |
" a.z(6)\n", | |
" \n", | |
" # In particular, this allows for passing callables to the original function\n", | |
" # by using decorator syntax. This is especially nice for passing multi-line lambdas.\n", | |
" @a.callback\n", | |
" def callback(ret):\n", | |
" print(ret)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "white-terrain", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment