[Python-3000] sets in P3K?
Barry Warsaw
barry at python.org
Thu Apr 27 22:44:51 CEST 2006
On Thu, 2006-04-27 at 11:49 -0700, 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.
Okay, radical/insane/heretical question time: do we really need literal
tuple syntax? IOW, what if (1, 2, 3) created a frozenset instead of a
tuple?
Or perhaps an even stupider idea: what if the compiler could recognize
certain use cases and generate different types depending on how the
object is used.
For example:
if key in ('one', 'two', 'three')
for x in (1, 2, 3, 5, 7, 11)
In both these cases, you don't really care if the literal syntax builds
a tuple or a frozenset. If you had duplicate entries you'd care, but
wouldn't that be something the compiler could figure out?
Now, in a case like the following, you /would/ want a tuple, but only to
preserve order:
x = (1, 2, 3)
foo(*x)
are tuple literals used enough this way, and what if you were forced to
use a list literal instead?
x = [1, 2, 3]
foo(*x)
So, again, what if (...) created a set/frozen set instead of a tuple?
It's py3k, so broken code be damned. :)
put-down-that-pitchfork-ly y'rs,
-Barry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 309 bytes
Desc: This is a digitally signed message part
Url : http://mail.python.org/pipermail/python-3000/attachments/20060427/a921e442/attachment.pgp
More information about the Python-3000
mailing list