tags:

views:

69

answers:

2

Is it possible to generate variables on the fly from a list?

In my program I am using the following instruction:

for i in re.findall(r"...(?=-)", str(vr_ctrs.getNodeNames())):
    tmp_obj = vr_ctrs.getChild(i+"-GEODE")

    TMP.append([tmp_obj.getPosition(viz.ABS_GLOBAL)[0],
                tmp_obj.getPosition(viz.ABS_GLOBAL)[1], 
                tmp_obj.getPosition(viz.ABS_GLOBAL)[2]])

which builds me a list on TMP. Would be possible to generate a new variable for each one of the elements that I am appending to TMP?

Thanks

+1  A: 

The global variables are in a dictionary that you can get by calling the globals() function.

globals()["foo"] = 17
foo  # => 17

But you should probably refactor your code to use objects, and then use setattr() instead.

dkagedal
Thanks for your answer, it really helps; but what do you mean by: But "you should probably refactor your code to use objects, and then use setattr() instead"???
relima
I assumed that your code was running at the top level in your Python file. If it was in fact running within a function, you might want to use `locals()` instead (I don't know what you want to do).
dkagedal
If i was going to create variables named from the matches of a regular express (e.g. if I was on the tail end of a week long drinking binge) I would probably use `locals()`
aaronasterling
... but this all seems like poor design. Why would you have code that dynamically creates variables? Variables are for you, the programmer, to create, and having the program create them behind your back only leads to confusion. Your program should handle data, and data belongs in data structures, just like the list in your example. If the list is not good enough for your purposes, you should use a better data structure, not spill the data on the floor by setting all kinds of variables. Creating an object and setting attributes in it (using setattr) is one way to do it.
dkagedal
@dkagedal. `locals()` works at any level. When at top level, `locals() == globals()`
aaronasterling
what he means is that though setting varialbes through `globals()` can work, it's not guaranteed by the language spec, this could change with a minor version update.
TokenMacGuy
vr_ctrs.getChild(i+"-GEODE") returns an object with its own properties. I need to be able to set these properties with a dot command. That is why I need to generate the variables.
relima
But maybe the setattr() will work as well. I ill give it a try.
relima
No, `globals()`is defined by the library specification. Changing it with a minor version update is not very likely.
dkagedal
But why not just set a temporary variable? `chld = vr_ctrs.getChild(...) ; chld.foobar`
dkagedal
If the attribute name ("foobar" in my example) is dynamically chosen, you should use `getattr()` and `setattr()`.
dkagedal
Thank you very much for your help. I managed to use these functions..
relima
+4  A: 

I would suggest that instead of creating variables, that you capture these entries in a dict, using what you would have used as the variable name for the dict keys. Then you can easily navigate through the parsed data by accessing dict.keys(), and you wont have to sort out your variables from other local or global variables. Also, if you happen to parse something that accidentally collides with a Python keyword (like 'for', for example), then using that as a dict key will still work, while using it as a variable or attribute name is not going to work.

Paul McGuire
This is awesome. I had forgotten that I could use a dictionary for storing the variables. It worked great, thanks.
relima