Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #46890
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <carlosnepomuceno@outlook.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.012 |
| X-Spam-Evidence | '*H*': 0.98; '*S*': 0.00; 'argument': 0.05; 'beginner': 0.05; 'method.': 0.07; 'variables': 0.07; 'string': 0.09; 'arguments': 0.09; 'function,': 0.09; 'type,': 0.09; 'subject:question': 0.10; 'answering...': 0.16; 'dict': 0.16; 'inconvenient': 0.16; 'literals': 0.16; 'literals,': 0.16; 'ok!': 0.16; 'received:65.55.116.7': 0.16; "{'a':": 0.16; 'wrote:': 0.18; 'variable': 0.18; 'resolved': 0.19; '+0000': 0.22; 'to:name :python-list@python.org': 0.22; 'received:65.55.116': 0.24; 'stick': 0.24; 'question': 0.24; "i've": 0.25; '>': 0.26; 'switch': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'function': 0.29; '[1]': 0.29; 'statement': 0.30; "i'm": 0.30; 'code': 0.31; '>>>>': 0.31; 'steven': 0.31; 'date:': 0.34; 'could': 0.34; 'case,': 0.35; 'but': 0.35; 'there': 0.35; 'keyword': 0.36; 'should': 0.36; 'email addr:python.org': 0.37; 'skip:& 10': 0.38; 'to:addr:python-list': 0.38; 'explain': 0.39; 'subject:': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'dangerous': 0.60; 'providing': 0.61; 'simple': 0.61; "you're": 0.61; "you've": 0.63; 're:': 0.63; 'such': 0.63; 'different': 0.65; 'email name:python-list': 0.65; 'design.': 0.68; 'as:': 0.81; 'dict()': 0.84; 'given.': 0.84; 'technically': 0.84; 'carlos': 0.91; 'on?': 0.91; '2013': 0.98 |
| X-TMN | [NhYI0kr+Jh6A0IU6A96rSBehz3sQTJ3r] |
| X-Originating-Email | [carlosnepomuceno@outlook.com] |
| Content-Type | multipart/alternative; boundary="_d97fec81-c685-486f-b2f0-cde4db1c0355_" |
| From | Carlos Nepomuceno <carlosnepomuceno@outlook.com> |
| To | "python-list@python.org" <python-list@python.org> |
| Subject | RE: Beginner question |
| Date | Tue, 4 Jun 2013 15:51:38 +0300 |
| Importance | Normal |
| In-Reply-To | <51addf2f$0$29966$c3e8da3$5496439d@news.astraweb.com> |
| References | <51addf2f$0$29966$c3e8da3$5496439d@news.astraweb.com> |
| MIME-Version | 1.0 |
| X-OriginalArrivalTime | 04 Jun 2013 12:51:38.0404 (UTC) FILETIME=[40C94E40:01CE6122] |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2644.1370350307.3114.python-list@python.org> (permalink) |
| Lines | 125 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1370350307 news.xs4all.nl 15913 [2001:888:2000:d::a6]:35522 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:46890 |
Show key headers only | View raw
[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