Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Setting variable values from dictionary

Reply
Thread Tools

Setting variable values from dictionary

 
 
Sean Berry
Guest
Posts: n/a
 
      04-23-2004
If I have a dictionary like the following:

{'category' : 2, 'shape', 4}

How can I use this to make
category = 2 and
shape = 4.

I want to be able to do this regardless of the dict values type. So:

{'cateogry' : 2, 'shape' : 'circle'}

will work as well.


Thanks in advance for any help. You guys rock.

--
 
Reply With Quote
 
 
 
 
Dave Kuhlman
Guest
Posts: n/a
 
      04-23-2004
Sean Berry wrote:

> If I have a dictionary like the following:
>
> {'category' : 2, 'shape', 4}
>
> How can I use this to make
> category = 2 and
> shape = 4.
>
> I want to be able to do this regardless of the dict values type.
> So:
>
> {'cateogry' : 2, 'shape' : 'circle'}
>
> will work as well.


locals().update(mydict)

Oops. I just actually read the docs:

locals( )
Update and return a dictionary representing the current
local symbol table. *Warning*: The contents of this
dictionary should not be modified; changes may not affect
the values of local variables used by the interpreter.

[Emphasis added.]

So, perhaps, the following is to be preferred:

for name, value in mydict.items():
exec('%s = %s' % (name, value))

But, what do you want to do about name clashes?

Dave

--
Dave Kuhlman
http://www.rexx.com/~dkuhlman
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Performance ordered dictionary vs normal dictionary Navkirat Singh Python 6 07-29-2010 10:18 AM
Re: Performance ordered dictionary vs normal dictionary Chris Rebert Python 0 07-29-2010 06:11 AM
creating a dictionary from a dictionary with regex james_027 Python 1 08-22-2007 07:39 AM
[DICTIONARY] - Copy dictionary entries to attributes Ilias Lazaridis Python 6 02-21-2006 11:27 AM
dictionary within dictionary Fox ASP General 5 03-13-2005 05:03 AM



Advertisments