Skip to content

Instantly share code, notes, and snippets.

@carymrobbins
Last active April 22, 2023 11:25
Show Gist options
  • Save carymrobbins/8940382 to your computer and use it in GitHub Desktop.
Save carymrobbins/8940382 to your computer and use it in GitHub Desktop.
Partial method for Python 2.7
from functools import partial
class partialmethod(partial):
def __get__(self, instance, owner):
if instance is None:
return self
return partial(self.func, instance,
*(self.args or ()), **(self.keywords or {}))
# Example usage:
class C(object):
def __init__(self, m):
self.m = m
def foo(self, x, y):
return self.m * (x + y)
bar = partialmethod(foo, 1)
c = C(2)
assert c.foo(1, 2) == 6
assert c.bar(3) == 8
@FooBarQuaxx
Copy link

Thanks. Very useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment