<p dir="ltr"><br>
On 4 Jun 2013 12:28, "Carlos Nepomuceno" <<a href="mailto:carlosnepomuceno@outlook.com">carlosnepomuceno@outlook.com</a>> wrote:<br>
><br>
> Started answering... now I'm asking! lol<br>
><br>
> I've tried to use dict() to create a dictionary to use like the switch statement providing variable names instead of literals, such as:<br>
><br>
> >>> a='A'<br>
> >>> b='B'<br>
> >>> {a:0,b:1}    #here the variables are resolved<br>
> {'A': 0, 'B': 1}<br>
><br>
> That's ok! But if I use dict() declaration:<br>
><br>
> >>> dict(a=0,b=1)<br>
> {'a': 0, 'b': 1}    #here variable names are taken as literals<br>
><br>
> What's going on? Is there a way to make dict() to resolve the variables?</p>
<p dir="ltr">Well yes. </p>
<p dir="ltr">dict(**{a:0,b:1})</p>
<p dir="ltr">The dict() constructor makes a dictionary from keyword arguments. So you just have to feed it keyword arguments using **.</p>
<p dir="ltr">And if you're in a bad day,</p>
<p dir="ltr">dict(**locals())</p>