Arithmetic sequences in Python

Paul Rubin http
Fri Jan 20 23:58:42 CET 2006


aleax at mail.comcast.net (Alex Martelli) writes:
> > How would you make a one-element list, which we'd currently write as [3]?
> > Would you have to say list((3,))?
> 
> Yep.  I don't particularly like the "mandatory trailing comma" in the
> tuple's display form, mind you, but, if it's good enough for tuples, and
> good enough for sets (how else would you make a one-element set?), 

If you really want to get rid of container literals, maybe the best
way is with constructor functions whose interfaces are slightly
different from the existing type-coercion functions:

    listx(1,2,3)  => [1, 2, 3]
    listx(3)      => [3]
    listx(listx(3)) => [[3]]
    dictx((a,b), (c,d))  => {a:b, c:d}
    setx(a,b,c)   => Set((a,b,c))

listx/dictx/setx would be the display forms as well as the constructor forms.



More information about the Python-list mailing list