
14 Aug
2020
14 Aug
'20
8:43 a.m.
Many times I want a function parameter that is an iterable but not a string. Usually I do:
try: var.__iter__ except AttributeError: # not an iterable else: try: var.isascii except AttributeError: # put yuour code here
or
from collections.abc import Iterable
if isinstance(var, Iterable) and not isinstance(var, str): # put yuour code here
The first example uses duck typing but it's more verbose. I use the first method in an home-made utility function.
I think it could be interesting to add a syntactic sugar to do this. Maybe a collections.notTextIterable() collections.nonTextIterable() collections.notStrIterable() collections.iterableNotStr() ?