Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #46890
| From | Carlos Nepomuceno <carlosnepomuceno@outlook.com> |
|---|---|
| Subject | RE: Beginner question |
| Date | 2013-06-04 15:51 +0300 |
| References | <51addf2f$0$29966$c3e8da3$5496439d@news.astraweb.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2644.1370350307.3114.python-list@python.org> (permalink) |
[Multipart message — attachments visible in raw view] - view raw
> From: steve+comp.lang.python@pearwood.info
> Subject: Re: Beginner question
> Date: Tue, 4 Jun 2013 12:35:59 +0000
> To: python-list@python.org
>
> On Tue, 04 Jun 2013 14:23:39 +0300, Carlos Nepomuceno 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?
>
>
> This is by design. You're calling a function, dict(), and like all
> functions, code like:
>
> func(name=value)
>
> provides a *keyword argument*, where the argument is called "name" and
> the argument's value is as given. dict is no different from any other
> function, it has no superpowers, keyword arguments are still keyword
> arguments.
>
> In this case, there is no simple way to use the dict() function[1] the
> way you want. You could build up a string and then call eval():
>
> s = "dict(%s=0, %s=1)" % (a, b)
> d = eval(s)
>
> but that's slow and inconvenient and dangerous if your data is untrusted.
>
> So in this specific case, you should stick to the {} method.
>
>
>
> [1] Technically it's a type, not a function, but the difference makes no
> difference here.
>
> --
> Steven
It's superclear now! You're an excelent teacher!
Can you explain me the difference of the type and function you've just mentioned?
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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