9

I would like to know how to convert a string input into a variable name to use into Python code. A concrete example:

def insrospect(foo, bar):
    requested_module = makestringvariable(foo)
    requested_object = makestringvariable(bar)
    import requested_module
    for item in inspect.getmemebers(requested_module.requested_object):
        member = makestringvariable(item[0])
        if callable(requested_object.member):
           print item

if __name__ == '__main__':
    introspect(somemodule, someobject)

So here above, because i do not know which module to introspect before launching, i need to convert the string to a usable module name and because getmembers() returns the members as strings, i also need them to be converted into usable variable names to check if they are callable.

Is there such a makestringvariable() function?

4
  • 1
    it's not very good idea to use "object" as a variable name, because it conflicts with Python2's "object" type or Python3's "object" class. Commented May 25, 2011 at 10:31
  • 1
    Please, please, please search: stackoverflow.com/search?q=%5Bpython%5D+string+variable+name. This question -- in this literal form -- has already been asked hundreds of times. Commented May 25, 2011 at 10:49
  • @S.Lott: i actually did look for these and looked into python doc since yesterday, but the questions ask here are to convert variable names into string and the one question regarding converting string to variable name is in fact about formating a string. Thanks for checking though, and if you can point to a questions the content of which answers my question i am ready to accept my mistake :) Commented May 25, 2011 at 10:56
  • @Benjamin: "but the questions ask [how] to convert variable names into string" That's clearly false. I guess you didn't really read them very carefully at all. stackoverflow.com/questions/1009831/…, for example, is a duplicate. Commented May 25, 2011 at 10:58

3 Answers 3

11

with the __import__ function and the getattr magic, you will be able to directly write this :

import importlib
def introspect(foo, bar):
    imported_module = importlib.import_module(foo)
    imported_object = getattr(imported_module, bar)
    for item in inspect.getmembers(imported_object):
        if callable(getattr(imported_object, item[0]):
           print item

if __name__ == '__main__':
    introspect(somemodule, someobject)
Sign up to request clarification or add additional context in comments.

5 Comments

__import__ won't return what you expect it to when the name contains dots, better to use importlib.import_module.
@Cat Plus Plus : great module, I used to split myself the parts of such import statements. I've learned something great :) and edited my answer.
Thanks it's exactly what i needed, amazing :) So i could even add some member to a class this way like getattr(self, string) = value?
@Benjamin : use setattr(self, string, value) instead of getattr to set a value ;)
édric: even better! :) -- i loove Python :D
6

You can't convert a string into a variable as such, because variables are part of your code, not of your data. Usually, if you have a need for "variable variables", as it were, you would use a dict:

data = {
    foo: foo_value,
    bar: bar_value
}

And then use data[foo] instead of trying to use foo as a variable. However, in this example you're actually asking about importing a module through a string, and about getting attributes using a string name, both of which are services Python provides: through the __import__ and getattr functions.

1 Comment

After reading your comment on my answer I removed my comment and my -1. The current -1 is not mine.
3

Members of a module are just attributes on that module, so you can use getattr on the module object to retrieve them.

The module objects themselves are stored in the sys.modules dictionary:

module = sys.modules[modulename]
member = getattr(module, membername)

2 Comments

If the module isn't imported yet, though, it won't exist in sys.modules either. You would need __import__ for that.
True enough, but I was assuming it's been imported already. :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.