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: 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 To: "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: <323f2f5b-1f50-4689-90b8-74c411e43971@googlegroups.com>, , <1ba2ceec-f778-4c95-bf98-260fc48b4c3f@googlegroups.com>, , , , <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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --_d97fec81-c685-486f-b2f0-cde4db1c0355_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable > From: steve+comp.lang.python@pearwood.info > Subject: Re: Beginner question > Date: Tue=2C 4 Jun 2013 12:35:59 +0000 > To: python-list@python.org >=20 > On Tue=2C 04 Jun 2013 14:23:39 +0300=2C Carlos Nepomuceno wrote: >=20 > > Started answering... now I'm asking! lol > >=20 > > I've tried to use dict() to create a dictionary to use like the switch > > statement providing variable names instead of literals=2C such as: > >=20 > >>>> a=3D'A' > >>>> b=3D'B' > >>>> {a:0=2Cb:1} #here the variables are resolved > > {'A': 0=2C 'B': 1} > >=20 > > That's ok! But if I use dict() declaration: > >=20 > >>>> dict(a=3D0=2Cb=3D1) > > {'a': 0=2C 'b': 1} #here variable names are taken as literals > >=20 > > What's going on? Is there a way to make dict() to resolve the variables= ? >=20 >=20 > This is by design. You're calling a function=2C dict()=2C and like all=20 > functions=2C code like: >=20 > func(name=3Dvalue) >=20 > provides a *keyword argument*=2C where the argument is called "name" and= =20 > the argument's value is as given. dict is no different from any other=20 > function=2C it has no superpowers=2C keyword arguments are still keyword= =20 > arguments. >=20 > In this case=2C there is no simple way to use the dict() function[1] the= =20 > way you want. You could build up a string and then call eval(): >=20 > s =3D "dict(%s=3D0=2C %s=3D1)" % (a=2C b) > d =3D eval(s) >=20 > but that's slow and inconvenient and dangerous if your data is untrusted. >=20 > So in this specific case=2C you should stick to the {} method. >=20 >=20 >=20 > [1] Technically it's a type=2C not a function=2C but the difference makes= no=20 > difference here. >=20 > --=20 > Steven It's superclear now! You're an excelent teacher! Can you explain me the difference of the type and function you've just ment= ioned? = --_d97fec81-c685-486f-b2f0-cde4db1c0355_ Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable


>=3B From: steve+= comp.lang.python@pearwood.info
>=3B Subject: Re: Beginner question
= >=3B Date: Tue=2C 4 Jun 2013 12:35:59 +0000
>=3B To: python-list@pyt= hon.org
>=3B
>=3B On Tue=2C 04 Jun 2013 14:23:39 +0300=2C Carlos= Nepomuceno wrote:
>=3B
>=3B >=3B Started answering... now I'm= asking! lol
>=3B >=3B
>=3B >=3B I've tried to use dict() to= create a dictionary to use like the switch
>=3B >=3B statement prov= iding variable names instead of literals=2C such as:
>=3B >=3B
&= gt=3B >=3B>=3B>=3B>=3B a=3D'A'
>=3B >=3B>=3B>=3B>=3B b= =3D'B'
>=3B >=3B>=3B>=3B>=3B {a:0=2Cb:1} #here the variable= s are resolved
>=3B >=3B {'A': 0=2C 'B': 1}
>=3B >=3B
>= =3B >=3B That's ok! But if I use dict() declaration:
>=3B >=3B >=3B >=3B>=3B>=3B>=3B dict(a=3D0=2Cb=3D1)
>=3B >=3B {'a':= 0=2C 'b': 1} #here variable names are taken as literals
>=3B >= =3B
>=3B >=3B What's going on? Is there a way to make dict() to res= olve the variables?
>=3B
>=3B
>=3B This is by design. You'= re calling a function=2C dict()=2C and like all
>=3B functions=2C cod= e like:
>=3B
>=3B func(name=3Dvalue)
>=3B
>=3B provid= es a *keyword argument*=2C where the argument is called "name" and
>= =3B the argument's value is as given. dict is no different from any other <= br>>=3B function=2C it has no superpowers=2C keyword arguments are still = keyword
>=3B arguments.
>=3B
>=3B In this case=2C there is= no simple way to use the dict() function[1] the
>=3B way you want. Y= ou could build up a string and then call eval():
>=3B
>=3B s =3D= "dict(%s=3D0=2C %s=3D1)" % (a=2C b)
>=3B d =3D eval(s)
>=3B
= >=3B but that's slow and inconvenient and dangerous if your data is untru= sted.
>=3B
>=3B So in this specific case=2C you should stick to = the {} method.
>=3B
>=3B
>=3B
>=3B [1] Technically i= t's a type=2C not a function=2C but the difference makes no
>=3B diff= erence here.
>=3B
>=3B --
>=3B Steven

It's supercle= ar now! You're an excelent teacher!

Can you explain me the differenc= e of the type and function you've just mentioned?

=
= --_d97fec81-c685-486f-b2f0-cde4db1c0355_--