[Tutor] Exec(uting) Code in a Dictionary?

Kent Johnson kent37 at tds.net
Sat Feb 7 21:40:43 CET 2009


On Sat, Feb 7, 2009 at 3:20 PM, Wayne Watson
<sierra_mtnview at sbcglobal.net> wrote:
> Is it possible to do some thing like this with exec:
>
> dcon0 = {'long':120.00, 'lat': 39.00, 'year': 2009}
>
> Can I use exec or eval simply on this to get:
> long = 120.00
> lat   = 39.00
> year = 2009
>
> That is, initialize each of these three variables.

Yes, without exec:
long = dcon0['long']
etc.

but why? If you know the keys, you can hard-code the assignments as
above. If you don't know the keys, you are probably better off leaving
the values in the dict.

Kent


More information about the Tutor mailing list