1

I would love to do something like this:

rates={'r1':10, 'r2':20}

and then do some cool command (what is it???) to end up with variables in my namespace

>>>r1 
10

>>>r2
20

Is this a dumb idea?

2
  • 3
    You are generally better sticking with the dictionary than trying to play with dynamic variables Commented Mar 26, 2014 at 23:28
  • Defining a dictionary and injecting it into the globals namespace is actually more typing than just doing r1=10;r2=20 etc Commented Mar 27, 2014 at 0:55

1 Answer 1

2

You can just do:

globals().update(rates)

Anyway, it's usually a bad practice.

globals()

Sign up to request clarification or add additional context in comments.

2 Comments

This won't work (unless locals() is globals()). Well, in some implementations of Python it might work, but at least in CPython it won't.
@WolframH: You're right, edited with globals(), that should be fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.