Here is an example with @functools.singledispatch
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)) test_items = [int(), str(), dict(), list()] for item in test_items: foo(item)
Output:that's not container <class 'int'>
Now that is container <class 'str'>
Now that is container <class 'dict'>
Now that is container <class 'list'>
EDIT: Fixed the import - collections.abc instead of just collections. Importing just collections was giving an error with 3.7, although at home I think I test the code with 3.5. collections.abc was part of collections before 3.3
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