Python Forum
testing if this is a container type
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
testing if this is a container type
#8
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

Reply


Messages In This Thread
testing if this is a container type - by Skaperen - Jan-24-2019, 07:01 PM
RE: testing if this is a container type - by buran - Jan-24-2019, 07:46 PM
RE: testing if this is a container type - by buran - Jan-24-2019, 07:57 PM
RE: testing if this is a container type - by buran - Jan-24-2019, 07:58 PM
RE: testing if this is a container type - by buran - Jan-25-2019, 07:12 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  uploading files from a ubuntu local directory to Minio storage container dchilambo 0 463 Dec-22-2023, 07:17 AM
Last Post: dchilambo
  Upload Files to Azure Storage Container phillyfa 6 706 Dec-22-2023, 06:11 AM
Last Post: Pedroski55
Lightbulb shutdown host from docker container cosmin1805 0 958 Nov-27-2022, 06:34 PM
Last Post: cosmin1805
  networkx package is not visible in singularity container image erdemath 11 2,297 Oct-14-2022, 12:04 PM
Last Post: Larz60+
  python installation/running inside singularity container erdemath 2 1,806 Sep-21-2022, 08:13 AM
Last Post: erdemath
  Python in Singularity Container on Ubuntu erdemath 0 912 Aug-31-2022, 02:17 PM
Last Post: erdemath
  UnUnloading values from multiple widgets in a container UGuntupalli 3 2,757 Apr-20-2020, 08:53 PM
Last Post: UGuntupalli
  Type hinting - return type based on parameter micseydel 2 2,500 Jan-14-2020, 01:20 AM
Last Post: micseydel
  How do I install Python 3.8.1 in a RHEL 8 UBI container? DevLinuxNC 1 3,625 Jan-08-2020, 09:37 PM
Last Post: Clunk_Head
  looking for a multi item container Skaperen 2 2,059 Apr-15-2019, 04:06 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020