Jan-25-2019, 07:12 AM
You can always register separate callback for str. It will take precedence over the callback for the abstract class. Also it will propagate for any custom class that inherit from str.
import collections.abc import functools @functools.singledispatch def foo(arg): print("that's not container", type(arg)) @foo.register(collections.abc.Container) def _(arg): print('Now that is container', type(arg)) @foo.register(str) def _(arg): print("that's str", type(arg)) class Foo(str): pass test_items = [int(), str(), dict(), list(), Foo()] for item in test_items: foo(item)
Output:that's not container <class 'int'>
that's str <class 'str'>
Now that is container <class 'dict'>
Now that is container <class 'list'>
that's str <class '__main__.Foo'>
>>>
Note that you can use different names for the registered callbacks so that you can distinguish and check which callback will be used for certain class.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs