8000 generate dispatch method only once · python/cpython@5277335 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5277335

Browse files
committed
generate dispatch method only once
1 parent a941bd6 commit 5277335

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

Lib/functools.py

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

934934
self.dispatcher = singledispatch(func)
935935
self.func = func
936+
self.attrname = None
936937

937938
def register(self, cls, method=None):
938939
"""generic_method.register(cls, func) -> func
@@ -949,12 +950,19 @@ def _method(*args, **kwargs):
949950
_method.__isabstractmethod__ = self.__isabstractmethod__
950951
_method.register = self.register
951952
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
952956
return _method
953957

954958
@property
955959
def __isabstractmethod__(self):
956960
return getattr(self.func, '__isabstractmethod__', False)
957961

962+
def __set_name__(self, owner, name):
963+
if self.attrname is None:
964+
self.attrname = name
965+
958966

959967
################################################################################
960968
### cached_property() - computed once per instance, cached as attribute

0 commit comments

Comments
 (0)
0