Revision 2611647c-b689-437b-a7ee-55fd4a344269 - Stack Overflow

You can resolve the value of `myobject`from the module where it is defined with `getattr`. If it is in the main module, this should work:

 import __main__

 mystring = 'This is a test.'

 def foo(object):
 print object

 variableName = 'mystring'
 foo(getattr(__main__, variableName))
 variableName = 'variableName'
 foo(getattr(__main__, variableName))

This should print

> This is a test.
>
> variableName

The import of the main module is necessary for variables from the main scope.

Edit: You can also get the content of the string with very dangerous `eval()`. Just replace `foo(getattr(__main__, variableName))` with `foo(eval(variableName))`.

AltStyle によって変換されたページ (->オリジナル) /