quote:
Original post by Zahlman
Oops, I was fixated on the other bit...
We all have our moments
quote:
@Kylotan: Can you do something with ''isinstance'' or something similar to determine if a Python object implements this ''sequence'' interface (and sorry for importing language terminology from elsewhere)?
The sequence interface is an abstract concept, but you
can use isinstance to determine if an object is of known sequence types since you can pass multiple types to isinstance:
>>> t = (1,2,3)>>> l = [1,2,3]>>> isinstance(t, (tuple,list))True>>> isinstance(l, (tuple,list))True
Alternatively, you could check the object dictionary for the methods that define the sequence interface. I can''t remember them all right now (and I''m too lazy/busy to look them up for you, sorry), but determining if an object implemented the iterator interface is as simple as checking for the existence of the __iter__ and next methods (the next method is actually a member of the object returned by __iter__, which can be self):
d = dir(file)if ''__iter__'' in d: print ''returns iterator, '',if ''next'' in d: print ''iteratable''
Extending that to sequences should be trivial.