Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #46875

RE: Beginner question

References (1 earlier) <vg3hahefhel.fsf@coffee.modeemi.fi> <1ba2ceec-f778-4c95-bf98-260fc48b4c3f@googlegroups.com> <CAPTjJmrM=ai3g2XXSSULYvxdJKhvoTe61PefFC3tc20i5ajhHA@mail.gmail.com> <kokblm$eb0$1@ger.gmane.org> <BLU176-W409F23043AF5028FCB3D50D79E0@phx.gbl>
Date 2013-06-04 12:34 +0100
Subject RE: Beginner question
From Fábio Santos <fabiosantosart@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2636.1370345675.3114.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

On 4 Jun 2013 12:28, "Carlos Nepomuceno" <carlosnepomuceno@outlook.com>
wrote:
>
> Started answering... now I'm asking! lol
>
> I've tried to use dict() to create a dictionary to use like the switch
statement providing variable names instead of literals, such as:
>
> >>> a='A'
> >>> b='B'
> >>> {a:0,b:1}    #here the variables are resolved
> {'A': 0, 'B': 1}
>
> That's ok! But if I use dict() declaration:
>
> >>> dict(a=0,b=1)
> {'a': 0, 'b': 1}    #here variable names are taken as literals
>
> What's going on? Is there a way to make dict() to resolve the variables?

Well yes.

dict(**{a:0,b:1})

The dict() constructor makes a dictionary from keyword arguments. So you
just have to feed it keyword arguments using **.

And if you're in a bad day,

dict(**locals())

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Beginner question eschneider92@comcast.net - 2013-06-03 20:39 -0700
  RE: Beginner question Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 09:46 +0300
    Re: Beginner question John Ladasky <john_ladasky@sbcglobal.net> - 2013-06-04 00:53 -0700
      RE: Beginner question Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 11:17 +0300
  Re: Beginner question Anssi Saari <as@sci.fi> - 2013-06-04 10:45 +0300
    Re: Beginner question John Ladasky <john_ladasky@sbcglobal.net> - 2013-06-04 00:57 -0700
      Re: Beginner question Chris Angelico <rosuav@gmail.com> - 2013-06-04 18:24 +1000
      Re: Beginner question Peter Otten <__peter__@web.de> - 2013-06-04 11:23 +0200
      RE: Beginner question Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 14:23 +0300
        Re: Beginner question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-04 12:35 +0000
          RE: Beginner question Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 15:51 +0300
            Re: Beginner question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-07 02:21 +0000
      RE: Beginner question Fábio Santos <fabiosantosart@gmail.com> - 2013-06-04 12:34 +0100
      RE: Beginner question Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 14:53 +0300
        Re: Beginner question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-04 12:25 +0000
          RE: Beginner question Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 15:37 +0300
          RE: Beginner question Fábio Santos <fabiosantosart@gmail.com> - 2013-06-04 13:47 +0100
      RE: Beginner question Fábio Santos <fabiosantosart@gmail.com> - 2013-06-04 13:15 +0100
      Re: Beginner question Mitya Sirenef <msirenef@lightbird.net> - 2013-06-04 13:16 -0400
  Re: Beginner question Larry Hudson <orgnut@yahoo.com> - 2013-06-04 02:13 -0700
    Re: Beginner question Roy Smith <roy@panix.com> - 2013-06-04 09:16 -0400
  Re: Beginner question Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-04 20:43 +0100
  Re: Beginner question eschneider92@comcast.net - 2013-06-05 20:42 -0700

csiph-web