Type Hierarchies

Burkhard Kloss bk at xk7.com
Wed Feb 14 06:12:20 EST 2001


Is there a way to check whether an object is a sequence? I'm not interested
in the specific type, just that it qualifies as a sequence, so it could be
built-in or user defined sequence. The language spec talks about type
hierarchies, but if there's any way to access that concept in code I must
have missed it.

Obviously I can try a call, and catch the exception:

def is_seq(a):
    try:
        len(a)
        return 1
    except:
        return 0

which works:

assert is_seq( [] )
assert is_seq( () )
assert is_seq( "" )
assert not is_seq( 1 )

but isn't very elegant.  It's probably also not very fast...

Am I missing something obvious here? Are there better ways?

Thanks,

    Burkhard







More information about the Python-list mailing list