8000 cache dispath method on singledispatch instance · python/cpython@abd1628 · GitHub
[go: up one dir, main page]

Skip to content

Commit abd1628

Browse files
committed
cache dispath method on singledispatch instance
1 parent ad5bd22 commit abd1628

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

Lib/functools.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ def __init__(self, func):
933933

934934
self.dispatcher = singledispatch(func)
935935
self.func = func
936-
self.attrname = None
936+
self._dispatch_method = None
937937

938938
def register(self, cls, method=None):
939939
"""generic_method.register(cls, func) -> func
@@ -943,25 +943,22 @@ def register(self, cls, method=None):
943943
return self.dispatcher.register(cls, func=method)
944944

945945
def __get__(self, obj, cls=None):
946+
if self._dispatch_method:
947+
return self._dispatch_method
948+
946949
def _method(*args, **kwargs):
947950
method = self.dispatcher.dispatch(args[0].__class__)
948951
return method.__get__(obj, cls)(*args, **kwargs)
949952

950953
_method.__isabstractmethod__ = self.__isabstractmethod__
951954
_method.register = self.register
952955
update_wrapper(_method, self.func)
953-
if obj is not None:
954-
# we set the method directly to the instance
955-
obj.__dict__[self.attrname] = _method
956956
return _method
957957

958958
@property
959959
def __isabstractmethod__(self):
960960
return getattr(self.func, '__isabstractmethod__', False)
961961

962-
def __set_name__(self, owner, name):
963-
if self.attrname is None:
964-
self.attrname = name
965962

966963

967964
################################################################################

0 commit comments

Comments
 (0)
0