Python Forum

Full Version: type for sequences
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
is there a common parent type for all sequence types that would make it simpler to test if some object is any sequence using isinstance() without having to construct a big list that varies between python2 and python3?
Well, I learned something just now:

import collections

for x in (list(), dict(), tuple(), set()):
    print(isinstance(x, (collections.abc.Mapping, collections.abc.Set, collections.abc.Sequence)))
python 3 for stullis:
>>> from collections import abc
>>> for x in (list(), dict(), tuple(), set()):
...     print(isinstance(x, (abc.Mapping, abc.Set, abc.Sequence))

Disregard,
Misread code
from collections import abc
all(isinstance(x, (abc.Mapping, abc.Set, abc.Sequence)) for x in (list(), dict(), tuple(), set(), str(), bytes(), bytearray(), frozenset()))