Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #33081 > unrolled thread
| Started by | Peter Otten <__peter__@web.de> |
|---|---|
| First post | 2012-11-10 10:19 +0100 |
| Last post | 2012-11-10 10:19 +0100 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Want to add dictionary keys to namespace? Peter Otten <__peter__@web.de> - 2012-11-10 10:19 +0100
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2012-11-10 10:19 +0100 |
| Subject | Re: Want to add dictionary keys to namespace? |
| Message-ID | <mailman.3531.1352539132.27098.python-list@python.org> |
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/2597278/python-load-variables-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}
Back to top | Article view | comp.lang.python
csiph-web