Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将python字符串转换为符号名_Python - Fatal编程技术网

将python字符串转换为符号名

将python字符串转换为符号名,python,Python,我想检查导入的名称。 代码: 输出: __builtins__ __cached__ __doc__ __file__ __name__ __package__ 现在我想检查每个已定义名称的值: 代码: 问题:如何从字符串中“提取”符号名? 假定代码: import fst #empty file fst.py for s in dir(fst): print(s, StrToSym(s)) # this call should be equal to str("__name__",

我想检查导入的名称。
代码:

输出:

__builtins__
__cached__
__doc__
__file__
__name__
__package__
现在我想检查每个已定义名称的值:
代码:

问题:如何从字符串中“提取”符号名?
假定代码:

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

使用
getattr
功能:

print(s, getattr(fst, s))

使用
getattr
功能:

print(s, getattr(fst, s))
使用:

使用:


没有所谓的“象征性名称”。模块包含的内容可以作为其属性使用,因此您需要的是
getattr(模块,名称)

没有“符号名称”这样的东西。模块包含的内容可以作为其属性使用,因此您需要的是
getattr(模块,名称)

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