1

I have an application that reads multiple variable names from one block of an input file. Then it reads the associated values from another block of the same file.. e.g. variable names read in as ABC,DEF,GEF,etc and values read in as 1,2,3,etc. In Python, can I create those variables at run time, e.g. perform the following code.

ABC = 1
DEF = 2
GHI = 3

I am quite new to Python so forgive if this is a stupid question...

martineau
  • 119,623
  • 25
  • 170
  • 301
  • Take at look at [this question](http://stackoverflow.com/q/5036700/1113211) and [this also.](http://stackoverflow.com/q/11354214/1113211) – salmanwahed Aug 17 '14 at 14:12
  • See [_Why you don't want to dynamically create variables_](http://stupidpythonideas.blogspot.com/2013/05/why-you-dont-want-to-dynamically-create.html). – martineau Aug 17 '14 at 16:17

3 Answers3

3

Sounds like you need a dictionary. A dictionary is a way of looking up values based on another value. In this case the values are the strings that you read from the file. For example:

myvars = {}
myvars['ABC'] = 1
myvars['DEF'] = 2
myvars['GHI'] = 3
Dale Myers
  • 2,703
  • 3
  • 26
  • 48
-1

In Python you technically can create global variables dynamically using globals:

globals()["ABC"] = 1

but it's something that is done rarely because most often a separate dictionary is a better choice (see Velox response). The function globals() simply returns the dictionary that Python uses to store global (module) variables.

Unless you're a Python expert and you really know what you're doing most probably using globals is a bad idea (however a Python expert wouldn't ask about it here).

Unfortunately there is not enough information in your question for me to guess what you're trying to accomplish by creating variables dynamically, so it's hard to provide a correct solution.

6502
  • 112,025
  • 15
  • 165
  • 265
  • Please don't recommend bad solutions. – Ned Batchelder Aug 17 '14 at 14:14
  • @NedBatchelder: and what is the bad solution I'm "recommending" ? – 6502 Aug 17 '14 at 22:21
  • I take your point: you aren't recommending to do things this way, merely pointing out that technically you can. Answers like this are an attractive nuisance: people jump to use this wrong thing because they don't know any better. – Ned Batchelder Aug 18 '14 at 00:49
-1

Velox is right, you should use a dictionary to store this kind of data:

vars = {}
vars['ABC'] = 1
print vars['ABC']            # 1

But just to answer your question, you have different ways to create variables at runtime:

eval

You can use eval to evaluate strings as if they were written in the source:

eval('ABC = 1')
print ABC                    # 1

namespace access

You can use globals() and locals() to access the global and local namespace (they are a dictionary). Then you can add another name with a value:

globals()['ABC'] = 1
print ABC                    # 1

Both these ways are not safe, in fact with eval you are executing external code that can be malicious or also crash your program. globals is the namespace of your program and messing with it could make it misbehave. So consider sticking to dictionaries.

Community
  • 1
  • 1
enrico.bacis
  • 30,497
  • 10
  • 86
  • 115
  • You're not supposed to modify the `locals()` dictionary, according to the [documentation](https://docs.python.org/2/library/functions.html#locals), so one shouldn't attempt to create local variables that way (if for no other reason). – martineau Aug 17 '14 at 16:35