Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,9 @@ functools

(Contributed by Yurii Karabas in :issue:`46014`.)

* :class:`functools.singledispatchmethod` exposes members 'registry' and
'dispatch'. (Contributed by Weipeng Hong in :issue:`42943`.)


.. _whatsnew311-gzip:

Expand Down
8 changes: 8 additions & 0 deletions Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,14 @@ def __wrapped__(self):
def register(self):
return self._unbound.register

@property
def dispatch(self):
return self._unbound.dispatcher.dispatch

@property
def registry(self):
return self._unbound.dispatcher.registry


################################################################################
### cached_property() - property result cached as instance attribute
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2853,6 +2853,13 @@ def _(self, arg):
self.assertEqual(a.arg, "base")
aa = A()
self.assertNotHasAttr(aa, 'arg')
def fun(self, arg):
pass
A.t.register(list, fun)
self.assertIs(fun, A.t.dispatch(list))
self.assertIs(fun, A().t.dispatch(list))
self.assertIs(fun, A.t.registry[list])
self.assertIs(fun, A().t.registry[list])

def test_staticmethod_register(self):
class A:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The :class:`functools.singledispatchmethod` exposes members 'registry' and
'dispatch'. Patch by Weipeng Hong.
Loading