[Python-3000] sets in P3K?
Ron Adam
rrr at ronadam.com
Thu Apr 27 22:05:08 CEST 2006
Raymond Hettinger wrote:
> [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
Yes, -1 on the {/}
I think I really prefer the {:} for an empty dictionary, and {} for an
empty set. I don't see where breaking this is any more of a problem
than many of the other items that are changing in P3K. Yes it's a more
commonly used item, but that is the point of the P3K project. To make
those changes that just aren't possible without breakage.
For those who want to write P3K compatible code in 2.x. They could
continue to write set() as they do now, and start writing dict() where
empty containers are needed. So this isn't an impossible situation.
It's also would not be difficult to run a search and replace on {} to
change it to {:} when the time comes.
I also think it would not be a problem to learn to use {:} for an empty
dict. An error would result fairly soon when someone tries to use {} as
a dict and it will be easy to remember dicts always contain at least one
':' in their spelling.
I think it's been suggested before that there should be a PY2.x to PY3.x
conversion utility to facilitate the move to PY3K. So maybe a sticking
point as when to *not* do something would be, if it's not possible to
translate a changed 2.x feature to a PY3K equivalent automatically
rather than if it will break 2.x code or not.
Cheers,
Ron
More information about the Python-3000
mailing list