Python Forum

Full Version: all types the same in a sequence
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
given a sequence i want to test if all items have the same type (any type) or not. is there a function to do this? an expression? a function that returns a sequence of unique items from a given sequence?
Here is my suggestion on this one:
same_type = more_itertools.all_equal(id(type(x)) for x in iterable)
what is the call to id() for?
i figured a way to avoid the need to do this test.
skaperen Wrote:what is the call to id() for?
I call id() because we're testing typeA is typeB and not typeA == typeB. I guess the latter will normally default to the former for type objects but I'm not certain of it in all circumstances. For example if the types have a metaclass where comparison is overridden, the behavior may change.