Beginner question

Mitya Sirenef msirenef at lightbird.net
Tue Jun 4 13:16:36 EDT 2013


On 06/04/2013 07:53 AM, Carlos Nepomuceno wrote:
> >On 4 Jun 2013 12:28,  "Carlos Nepomuceno" <carlosnepomuceno at outlook.com> wrote:
 > [...]
 > >> 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())
 >
 > That's exactly the same!
 > >>>dict(**{a:0,b:1})=={a:0,b:1}
 > True
 >
 > Are there any benefits from using dict() instead of {}?
 >


Other than what Steven already mentioned, a big advantage is that it's
easier to make a dict if you have a lot of keys that are valid
identifiers, and it's more readable, to boot:

dict(red=1, blue=2, orange=3, violet=4, crimson=5, ...)

VS.

{'red':1, 'blue':2, 'orange':3, 'violet':4, 'crimson':5, ...}

  -m


-- 
Lark's Tongue Guide to Python: http://lightbird.net/larks/

Although the most acute judges of the witches and even the witches
themselves, were convinced of the guilt of witchery, the guilt nevertheless
was non-existent. It is thus with all guilt.  Friedrich Nietzsche




More information about the Python-list mailing list