set issubset performance difference depending on the argument type

前端 未结 1 557
失恋的感觉
失恋的感觉 2021-01-19 05:42

why this question ?

I was trying to answer this question: Check if All Values Exist as Keys in Dictionary with something better than a generator com

相关标签:
1条回答
  • 2021-01-19 06:16

    The set.issubset algorithm requires a set to work with (frozensets and subclasses count); if you pass it something else, it will make a set. It's basically all(elem in other for elem in self), and it needs to know that elem in other is efficient and means what it means for sets. The only way it knows how to guarantee that is to ensure other is a set. Making a set is expensive.

    (I've glossed over some details. If you want to know exactly what's going on, particularly if you have a weird set subclass, read the source code in the link.)

    0 讨论(0)
提交回复
热议问题