Skip to content

Instantly share code, notes, and snippets.

@jace
Created May 10, 2020 07:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jace/9897629abda9bbd06f5a1bf862f43d42 to your computer and use it in GitHub Desktop.
Save jace/9897629abda9bbd06f5a1bf862f43d42 to your computer and use it in GitHub Desktop.
Performance difference of Python's partial vs partialmethod
from functools import partial, partialmethod
from timeit import timeit
class Partial(Base):
def __init__(self):
self.p = partial(self.foo, 1)
class PartialMethod(Base):
p = partialmethod(Base.foo, 1)
# `partial` is significantly faster than `partialmethod` despite
# repeated instantiation (reconfirmed by reversing test order).
print(timeit('p = Partial(); p.p()', globals=globals()))
print(timeit('p = PartialMethod(); p.p()', globals=globals()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment