[Python-3000] [Python-Dev] Python 3000 Status Update (Long!)

Josiah Carlson jcarlson at uci.edu
Sun Jun 24 23:05:30 CEST 2007


Brandon Craig Rhodes <brandon at rhodesmill.org> wrote:
> Joachim König <him at online.de> writes:
> 
> > ... could someone enlighten me why
> >
> > {,}
> >
> > can't be used for the empty set, analogous to the empty tuple (,)?
> 
> And now that someone else has broken the ice regarding questions that
> have probably been exhausted already, I want to comment that Python 3k
> seems to perpetuate a vast asymmetry.  Observe:

Since no one seems to have responded to this, I will go ahead and do so
(I just got back from vacation).


> (a) Syntactic constructors
> 
>  [ 1,2,3 ]   works
>  { 1,2,3 }   works
>  { 1:1, 2:4, 3:9 }   works
> 
> (b) Generators + constructor functions
> 
>  list(i for i in (1,2,3))   works
>  set(i for i in (1,2,3))   works
>  dict((i,i*i) for i in (1,2,3))   works
> 
> (c) Comprehensions
> 
>  [ i for i in (1,2,3) ]   works
>  { i for i in (1,2,3) }   works
>  { i:i*i for i in (1,2,3) ]   returns a SyntaxError!

But you forgot tuples!

    ( 1,2,3 )
    tuple(i for i in (1,2,3))
    (i for i in (1,2,3))

Oops, that last one isn't a tuple, it is a generator expression wrapped
up in parenthesis.  Really though, there are two exceptions to the rule.
Honestly, if you are that concerned about teaching students the language
(to the point that they have difficulty figuring out the *two*
exceptions to the rule), teach them the single form that always works;
generators + constructors.  They may see the three different
comprehensions/expressions (list, set, generator), but it should be
fairly easy to explain that they are equivalent to the generator +
constructor version.

> Given that Python 3k is making such strides in other areas where cruft
> and asymmetry needed to be removed, it would seem a shame to leave the
> container types in such disarray.

And one could make the argument that TOOTDI says that literals and
generators + constructors are the only reasonable options.
Comprehensions save perhaps 5 characters over the constructor method,
and may be a bit faster, but result in teh asymmetry above.  But I will
admit that comprehension syntax is not likely to be going anywhere, and
dictionary comprehensions are not likely to be added (and neither are
tuple comprehensions).

 - Josiah



More information about the Python-3000 mailing list