creating dictionnaries

Michael P. Reilly arcege at shore.net
Tue Jun 22 23:01:25 CEST 1999


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.





More information about the Python-list mailing list