Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Revisions

4 of 4
Commonmark migration

You can resolve the value of myobjectfrom 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)).

clemens
  • 17.9k
  • 12
  • 52
  • 71

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