all
all

Reputation: 324

python string to symbolic name

I want to check imported names.
Code:

import fst # empty file fst.py
for s in dir(fst):
    print(s) # got string with symbolic names

Output:

__builtins__
__cached__
__doc__
__file__
__name__
__package__

Now I want to check values of each defined name:
Code:

print(__builtins__)
print(__cached__)
print(__doc__)
print(__file__)
print(__name__)
print(__package__)

Question: How can I "extract" symbolic name from string?
supposed code:

import fst #empty file fst.py
for s in dir(fst):
    print(s, StrToSym(s)) # this call should be equal to str("__name__", __name__)

Upvotes: 0

Views: 300

Answers (3)

Cat Plus Plus
Cat Plus Plus

Reputation: 130014

There's no such thing as a "symbolic name". What module contains is available as its attributes, so getattr(module, name) is what you're looking for.

Upvotes: 0

phihag
phihag

Reputation: 288298

Use getattr:

for s in dir(fst):
  print (getattr(fst, s))

Upvotes: 2

Marcelo Cantos
Marcelo Cantos

Reputation: 186118

Use the getattr function:

print(s, getattr(fst, s))

Upvotes: 1

Related Questions