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

Ka-Ping Yee ping@lfw.org
2001年1月22日 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

AltStyle によって変換されたページ (->オリジナル) /