Is there a canonical way to check whether an iterable is ordered?
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Fri Sep 19 01:04:51 EDT 2014
cool-RR wrote:
> My function gets an iterable of an unknown type. I want to check whether
> it's ordered. I could check whether it's a `set` or `frozenset`, which
> would cover many cases, but I wonder if I can do better. Is there a nicer
> way to check whether an iterable is ordered or not?
See the collections.abc module:
https://docs.python.org/3/library/collections.abc.html
I think what you want is:
import collections.abc
isinstance(it, collections.abc.Sequence)
Prior to 3.3, you would use:
# Untested.
import collections
isinstance(it, collections.Sequence)
--
Steven
More information about the Python-list
mailing list