Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.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.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'variables': 0.07; 'arguments': 0.09; 'constructor': 0.09; 'subject:question': 0.10; 'cc:addr:python-list': 0.11; 'answering...': 0.16; 'literals': 0.16; 'literals,': 0.16; 'ok!': 0.16; "{'a':": 0.16; 'wrote:': 0.18; 'variable': 0.18; 'resolved': 0.19; '>>>': 0.22; 'cc:addr:python.org': 0.22; '>>>': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; "i've": 0.25; '>': 0.26; 'switch': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'yes.': 0.31; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'keyword': 0.36; 'feed': 0.38; 'bad': 0.39; 'providing': 0.61; "you're": 0.61; 'such': 0.63; 'skip:n 10': 0.64; 'as:': 0.81; 'dict()': 0.84; 'on?': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=3ykr/T91XiN7yDm7oYVHAJ+avKdsOv7inZGsk/X/xbM=; b=YOpM8sR9omrbZtwiU67yqWhZp5kTn4XNiIT1DLF71LnFteZ6ygn7RXX43DXf7FD0id p29GLLQ3E2a+Oodurl7Q0WUCqWwFZ/XZkqRZGyTIBeAIQxi1n0Hl0ihV5RljEMMpXAqg 5khjimQ31WrWtVAb+LaNY/hZhuMhuRUOkhFq4c+X4pymzWhrr228Q71PmNg03zJucrPw t9dSD4i7SbNNaRQan7yj19vCdbWPs3MkQWQ8UxHh0m15fpMvbpxJQDVZWtmdeXfx4DCw bObNKNJzZl6p628ub6ek2iPNsUdGtXGsDzFrwUZLMnKO5zNRN/TkmbpzVVXZMFpU3mJG csdQ== MIME-Version: 1.0 X-Received: by 10.49.62.72 with SMTP id w8mr26499454qer.22.1370345666559; Tue, 04 Jun 2013 04:34:26 -0700 (PDT) In-Reply-To: References: <323f2f5b-1f50-4689-90b8-74c411e43971@googlegroups.com> <1ba2ceec-f778-4c95-bf98-260fc48b4c3f@googlegroups.com> Date: Tue, 4 Jun 2013 12:34:26 +0100 Subject: RE: Beginner question From: =?ISO-8859-1?Q?F=E1bio_Santos?= To: Carlos Nepomuceno Content-Type: multipart/alternative; boundary=047d7bdc0944acc68104de527618 Cc: python-list@python.org 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: 68 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370345675 news.xs4all.nl 15897 [2001:888:2000:d::a6]:37333 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:46875 --047d7bdc0944acc68104de527618 Content-Type: text/plain; charset=ISO-8859-1 On 4 Jun 2013 12:28, "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? 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()) --047d7bdc0944acc68104de527618 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable


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 sw= itch statement providing variable names instead of literals, such as:
>
> >>> a=3D'A'
> >>> b=3D'B'
> >>> {a:0,b:1}=A0=A0=A0 #here the variables are resolved
> {'A': 0, 'B': 1}
>
> That's ok! But if I use dict() declaration:
>
> >>> dict(a=3D0,b=3D1)
> {'a': 0, 'b': 1}=A0=A0=A0 #here variable names are tak= en as literals
>
> What's going on? Is there a way to make dict() to resolve the vari= ables?

Well yes.

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

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

And if you're in a bad day,

dict(**locals())

--047d7bdc0944acc68104de527618--