creating dictionnaries

Michael Hudson mwh21 at cam.ac.uk
Wed Jun 23 02:49:22 EDT 1999


"Michael P. Reilly" <arcege at shore.net> writes:

> Christian Caremoli <Christian.Caremoli at der.edf.fr> wrote:
> : 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 ?
> 
> In a word, No.  The structure of the language would perform a namespace
> lookup on "a" and "b" instead of taking them as strings.  Also a tuple
> is still an expression, and Python does not allow assignments inside
> expressions.  This means that you would get a SyntaxError exception
> during bytecode compilation.  And even if you didn't, it would be
> likely that you would get a NameError exception.
> 
>   -Arcege
> 
> Note: I haven't looked at bytecodehacks, but that might let you do what
> you wish.  Since it would be extremely non-portable, I wouldn't suggest
> it for published or long-term code.

What? byecodehacks can't affect syntax! I'm quite proud of that bit of
code, but lets not blow it up into things it just isn't...

yours,
Michael




More information about the Python-list mailing list