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?
String Into Variable Name in Python Using the vars() Function. Instead of using the locals() and the globals() function to convert a string to a variable name in python, we can also use the vars() function. The vars() function, when executed in the global scope, behaves just like the globals() function.
To get a variable's name as a string: Use the globals() function to get a dictionary that implements the current module namespace. Iterate over the dictionary to get the matching variable's name. Access the list item at index 0 to get the name of the variable.
Declaring strings as variables can make it easier for us to work with strings throughout our Python programs. To store a string inside a variable, we need to assign a variable to a string. In this case let's declare my_str as our variable: my_str = "Sammy likes declaring strings."
Well, it is possible, and here in this simple tutorial, I am going to show you how to convert any string into a variable in JavaScript. To do this task, we will use the JavaScript eval() function. Well, this is the function that will play the main role to create variable name from our string.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With