Mailing List Archive

creating dictionnaries
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
creating dictionnaries [ In reply to ]
Christian Caremoli 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 ?

You betcha. Just build a function like this one:

def dict(**kwargs):
return kwargs

and shazam! You've got your keyword args style dictionary creator.

ex:
>>> dict(a=5, b='jim')
{'b':'jim', 'a':5}


-Jim.
creating dictionnaries [ In reply to ]
On 22 Jun 99, Christian Caremoli 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 ?

Nope. :-) I wonder why you would want this? As far as I'm concerned,
there's not screamingly much difference between

d=f(a=1,b=2) # valid

and

d=(a=1,b=2) # invalid


--Hans Nowak (ivnowa@hvision.nl)
Homepage: http://fly.to/zephyrfalcon
creating dictionnaries [ In reply to ]
Christian Caremoli <Christian.Caremoli@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.
creating dictionnaries [ In reply to ]
Christian Caremoli <Christian.Caremoli@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
creating dictionnaries [ In reply to ]
Thanks for all the answers
I know that it s invalid and what I use now is a function named _F to
avoid name clashes
def _F(**args):
return args
What I want is to have an uniform syntax to write :
a=mycommand(x=1,y=2,z=(d=1,e=2),s=1)
or alike
instead of
a=mycommand(x=1,y=2,z=_F(d=1,e=2),s=1)
I wonder if it exists any syntactic sugar or any trick that could help me


Hans Nowak wrote:

> On 22 Jun 99, Christian Caremoli 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 ?
>
> Nope. :-) I wonder why you would want this? As far as I'm concerned,
> there's not screamingly much difference between
>
> d=f(a=1,b=2) # valid
>
> and
>
> d=(a=1,b=2) # invalid
>
> --Hans Nowak (ivnowa@hvision.nl)
> Homepage: http://fly.to/zephyrfalcon
creating dictionnaries [ In reply to ]
"Michael P. Reilly" <arcege@shore.net> writes:

> Christian Caremoli <Christian.Caremoli@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
creating dictionnaries [ In reply to ]
Michael Hudson <mwh21@cam.ac.uk> wrote:
: "Michael P. Reilly" <arcege@shore.net> writes:

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

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.

-Arcege

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