Doing exactly what you require involves finding O (n) in the character table, which is terrible IMHO.
If you can pass the string corresponding to the variable name, you can do this:
import sys
def myprint(name, mod=sys.modules[__name__]):
print('{}: {}'.format(name, getattr(mod, name)))
Test:
a=535
b='foo'
c=3.3
myprint('a')
myprint('b')
myprint('c')
It will be printed:
a: 535
b: foo
c: 3.3
You can also use it to print variables from another module by passing a second argument, for example:
>>> import os
>>> myprint('pathsep', os)
pathsep: :
source
share