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

Return to Answer

Commonmark migration
Source Link

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)).

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)).

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)).

added 166 characters in body
Source Link
clemens
  • 17.9k
  • 12
  • 52
  • 71

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)).

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.

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)).

use variable instead of constant string
Source Link
clemens
  • 17.9k
  • 12
  • 52
  • 71

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__, 'mystring'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.

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
foo(getattr(__main__, 'mystring'))

This should print

This is a test.

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

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.

Source Link
clemens
  • 17.9k
  • 12
  • 52
  • 71
Loading
lang-py

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