So it turned out that I needed a way of ensuring that all members of an iterable are instances of a certain class. Here, therefore, is the isallinstance()
function.
1 2 3 4 5 6 7 8 9 10 11 | def isallinstance(iterable, typeOrTuple):
return all(isinstance(i, typeOrTuple) for i in iterable)
## Previous Version:
##
## def isallinstance(iterable, typeOrTuple):
## vals = [isinstance(i, typeOrTuple) for i in iterable]
## return len(vals) == sum(vals)
|
How about this version written for Python 3?
I've never seen the all() code before, but fiddling around suggests it's been around since 2.5, which makes it worrying that I've never noticed it before. Thanks.