Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
functools.partial workalike where provided positionals are appended, not prepended.
import functools
class partial_last(functools.partial):
"""Like functools.partial, but fill positionals from the end.
Useful for builtin C functions where you *can't* pass later args as
keywords because CPython is stupid about that.
>>> int_checker = partial_last(isinstance, int)
>>> int_checker(5)
>>> int_checker('Bob')
>>> foo_attr_getter = partial_last(getattr, 'foo', 'no way jose')
>>> class Bar(object):
... foo = 'yes sirree'
>>> foo_attr_getter(Bar())
'yes sirree'
>>> foo_attr_getter(6)
'no way jose'
def __call__(self, *args, **kwargs):
all_kw = dict(self.keywords) if self.keywords is not None else {}
return self.func(*(args + self.args), **all_kw)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment