The function matplotlib.cbook.iterable has the documentation:
def iterable(obj):
'return true if *obj* is iterable'
try: len(obj)
except: return False
return True
However, in Sage, we have some objects that have __len__ defined, but are not iterable (i.e., they don't implement the iterator protocol). This is causing us problems when we try to plot some things that use this function, and matplotlib falsely assumes that the things are iterable. After checking around online, it seems that it is safer to check for iterability by doing something like:
try: iter(obj)
except TypeError: return False
return True
or
import collections
return isinstance(obj, collections.Iterable) # only works for new-style classes
Or maybe even combining these would be better (though it might be really redundant and slow, after looking at the code in collections.Iterable...):
try: iter(obj)
except TypeError:
import collections
return isinstance(obj, collections.Iterable)
return True
You guys are the python experts, though. What do you think?
Thanks,
Jason