Python Forum
all types the same in a sequence
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
all types the same in a sequence
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Here is my suggestion on this one:
same_type = more_itertools.all_equal(id(type(x)) for x in iterable)
Reply
#3
what is the call to id() for?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
i figured a way to avoid the need to do this test.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020