我知道使用 globals()、locals() 和 getattr 来通过字符串引用 Python 中的东西(如在这个问题中),但除非我遗漏了一些明显的东西,否则我似乎无法将它与调用类型一起使用。
例如:
In [12]: locals()['int']
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
e:\downloads_to_access\<ipython console> in <module>()
KeyError: 'int'
In [13]: globals()['int']
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
e:\downloads_to_access\<ipython console> in <module>()
KeyError: 'int'
getattr(???, 'int')...
这样做的最佳方法是什么?