[Python-3000] sets in P3K?

Raymond Hettinger rhettinger at ewtllc.com
Thu Apr 27 20:49:27 CEST 2006


[pep-3100 checkin]
>  {F(x) for x in S if P(x)} means set(F(x) for x in S if P(x)).

I presume this means that there will never be dictionary comprehensions (as they would aspire to have an identical notation).



>  There's no frozenset literal; they are too rarely needed.

Actually, they are one of the most common use cases for set literals.  Whenever someone writes code like, "
if ext in {'html', 'xml, 'xhtml'}: do_something()", they are intending to create an immutable set that could possibly be optimized into a constant by the compiler.  Currently, folks don't bother writing-in the frozen-part because they know that no optimization currently takes place.



>  The {/} part is still controversial.

I suspect this is going to be a wart.  One of the reasons for the class-keyword to now accept an emtpy list of bases is the difficulties arising from deleting a base class and leaving an invalid syntax.  The same is true for sets.  Start with {1,2}, delete an element leaving {1}, and delete another element leaving {}, oops the type just changed.

I forsee {} vs {/} as being a prominent entry on everyone's list of Python pitfalls, faqs, and common errors.  Maybe PyLint or PyChecker will be able to scan to see how the object is used and determine whether the two were mixed-up.


Raymond





More information about the Python-3000 mailing list