creating dictionnaries

Michael P. Reilly arcege at
Wed Jun 23 06:50:26 EDT 1999

Michael Hudson <mwh21 at> wrote:
: "Michael P. Reilly" <arcege at> writes:

:> Christian Caremoli <Christian.Caremoli at> 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

I only said might.. my thought was to splice a CALL_FUNCTION op in
where the expression was.  I did say that I haven't looked at the
module so I wasn't sure of it's capabilities, or if it worked inside
the parser or in the eval loop, but it seemed to be a reasonably
possible capability.


PS: From what I've heard of the module, you should be proud of it.

More information about the Python-list mailing list