0

It doesn't seem that it's possible to use a string as the name of a variable. I basically want the program to understand that the thing inside the quote marks is the name of an object I want to reference. I want to do something like this:

str = "modname"

import var(str)

var() is a fictional function. I'm using it in the way one might use str() or int(). I imagine you might ask "Why?" It's so I can do something like this:

class player:
    __init___():
        attributes = [ "hp", "attack", "defense" ]
        for x in range( 0, len(self.attributes) ]:
            self.var(attributes[x]) = var( attributes[x] + "()" )

(Assuming you've already done something like from stats import * ). But the real payoff would be the ability to do something like this:

for x in range( 0, len(self.attributes) ):
    self.var(attributes[x]).modAttr(-5)
    print self.var(attributes[x]).magnitude

Is this possible? Is it completely absurd to want to do this? Or is there a different, more canon way to perform such a task?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
A B
  • 59
  • 6
  • possible duplicate of [How can you dynamically create variables in Python via a while loop?](http://stackoverflow.com/questions/5036700/how-can-you-dynamically-create-variables-in-python-via-a-while-loop) : use a dict – fredtantini Dec 01 '14 at 08:12
  • I guess that would help a little, but doesn't it seem silly to make a dictionary like `attributes = { 'hp' : hp, 'attack' : attack }` when there should be a way to just say `take_off_the_quote_marks('hp')` ? (I don't think this is a duplicate.) – A B Dec 01 '14 at 08:35
  • *"Is this possible?"* Yes, that's what `getattr` and `setattr` are for. *"Is it completely absurd to want to do this?"* Hard to say; it's not clear what, exactly, you're trying to do. Your `player.__init__` includes a `NameError` and seems to be trying to assign to the attributes a call to themselves. Also, why bother with the index `x`? `for attr in attributes:` is much more readable. – jonrsharpe Dec 01 '14 at 08:36
  • This is the answer I was looking for. I just needed someone to point me to `getattr`/`setattr`. Thanks. Disregard the quality of my code, this is just a hasty example. – A B Dec 01 '14 at 08:47
  • 1
    The code is supposed to dynamically instantiate modules from a list of strings. It's confusing because I gave the instances the same names at the objects. Turns out my question is actually a duplicate of this one: http://stackoverflow.com/questions/4821104/python-dynamic-instantiation-from-string-name-of-a-class-in-dynamically-imported – A B Dec 01 '14 at 08:55
  • What jonrsharpe said. As for importing a module when you have the module name as a string, you _could_ use [importlib.import_module](https://docs.python.org/3/library/importlib.html#importlib.import_module). Note that I'm _not_ suggesting that this is necessarily a good idea. :) – PM 2Ring Dec 01 '14 at 12:09

1 Answers1

0

I found the answer. Turns out it's based on something like this.

class whatever:
    list_of_modules = [ "one", "two", "three" ]
    for l in list_of_modules:
        exec ("self.attr_%s = %s()" % ( l, l ))
A B
  • 59
  • 6