Re: Want to add dictionary keys to namespace?

Discussion in 'Python' started by Chris Angelico, Nov 10, 2012.

  1. On Sat, Nov 10, 2012 at 11:00 AM, 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.
    >
    > #Do this?
    > dictionary = {"AppleSeed": None, "Has": None,"Horrible" :None,"Art"}
    > for key in dictionary.keys():
    > eval("%s=None"%key)


    I would strongly recommend not.

    > #or do this?
    > locals().update(dictionary)


    That doesn't work in a function, but outside of a function, you should
    be able to use:

    globals().update(dictionary)

    However, I would advise using the dictionary explicitly. Give it a
    shorter name and it'll be easier, but don't go for namespace
    pollution. The PHP folks finally realized that register_globals is a
    bad idea.

    ChrisA
     
    Chris Angelico, Nov 10, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sandeep Kanwal

    serial keys/validation keys

    sandeep Kanwal, Oct 29, 2004, in forum: C++
    Replies:
    1
    Views:
    597
    Mike Wahler
    Oct 29, 2004
  2. Harry George
    Replies:
    9
    Views:
    735
    sonal
    Jun 13, 2006
  3. Replies:
    10
    Views:
    749
    Daniel T.
    Feb 3, 2006
  4. Deep Mehta via .NET 247
    Replies:
    2
    Views:
    463
    Dave A
    May 31, 2005
  5. Peter Otten
    Replies:
    0
    Views:
    178
    Peter Otten
    Nov 10, 2012
Loading...

Share This Page