creating dictionnaries

Timothy R Evans tre17 at cosc.canterbury.ac.nz
Tue Jun 22 18:21:37 EDT 1999


Christian Caremoli <Christian.Caremoli at der.edf.fr> writes:

> Hi,
> Dictionnaries can be created like that :
> d={'a':1,'b':2}
> 
> By calling a function you create a dictionnary like that :
> def f(**d):
>    return d
> 
> d=f(a=1,b=2)
> 
> I would like to be able to create dictionnaries with some similar syntax
> like keyed tuples :
> d=(a=1,b=2)
> 
> Is there a way to do that ?
> 
> Thanks
> 
> Christian Caremoli

I don't think so, there's no equivalent operator to Perl's =>.  You
cound just use your function, but this introduces some restrictions on
the dictionary keys, the most important of which is that they must be
valid identifiers and will always be strings.  There tend to be many
situations where you want the key to cotain spaces or other characters
that are not allowed in identifiers, or where you want the key to be
an integer or tuple etc.

--
Tim Evans




More information about the Python-list mailing list