A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2001-January/012047.html below:

[Python-Dev] Is X a (sequence|mapping)?

[Python-Dev] Is X a (sequence|mapping)?Ka-Ping Yee ping@lfw.org
Mon, 22 Jan 2001 20:22:56 -0800 (PST)
We can implement abstract interfaces (sequence, mapping, number) in
Python with the appropriate __special__ methods, but i don't see an
easy way to test if something supports one of these abstract interfaces
in Python.

At the moment, to see if something is a sequence i believe i have to
say something like

    try:
        x[0]
    except:
        # not a sequence
    else:
        # okay, it's a sequence

or

    if hasattr(x, '__getitem__') or type(x) in [type(()), type([])]:
        ...

Is there, or should there be, a better way to do this?



-- ?!ng




RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4