Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Want to add dictionary keys to namespace?

Reply
Thread Tools

Re: Want to add dictionary keys to namespace?

 
 
Peter Otten
Guest
Posts: n/a
 
      11-10-2012
Jeff Jeffries wrote:

> Smart people, Is there a way I can add a dictionaries keys to the python
> namespace? It would just be temporary as I am working with a large
> dictionary, and it would speed up work using an IDE. I look and find
> nothing... none of the keys have spaces and none are common names within
> the module.
>
> http://stackoverflow.com/questions/2...ariables-in-a-

dict-into-namespace
>
> I do this:
>
> #Do this?
> dictionary = {"AppleSeed": None, "Has": None,"Horrible" :None,"Art"}
> for key in dictionary.keys():
> eval("%s=None"%key)
>
> #or do this?
> locals().update(dictionary)
>
> Any ideas?


You could instead use a dict subclass that lets you access values as
attributes:

>>> class Dict(dict):

.... def __getattr__(self, name):
.... return self[name]
.... def __setattr__(self, name, value):
.... self[name] = value
....
>>> d = Dict({"AppleSeed": None, "Has": None, "Horrible" : None, "Art": 42})
>>> d.Art

42
>>> d.AppleSeed
>>> d.AppleSeed = "spam"
>>> d

{'Has': None, 'Art': 42, 'AppleSeed': 'spam', 'Horrible': None}


 
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
Re: Want to add dictionary keys to namespace? Chris Angelico Python 0 11-10-2012 12:52 AM
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



Advertisments