使用类方法导入模块

2024-04-26 23:37:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用import模块和getattr获取类方法的docstring。我以前做过以下工作:

getattr(import_module('string'),'capwords').__doc__

哪一个像预期的那样有效? 现在我尝试对类方法执行相同的操作,并以this为例

getattr(import_module('string','Formatter'),'format').__doc__

我得到以下错误

Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: 'module' object has no attribute 'format'

如何获取类方法的doc字符串。你知道吗


Tags: 模块方法importformatmoststringdocformatter
2条回答

感觉你在努力

getattr(getattr(import_module('string'),'Formatter'), 'format').__doc__

或者

from operator import attrgetter
attrgetter("Formatter.format")(import_module("string")).__doc__

要获取^{}方法的docstring,可以执行以下操作:

from string import Formatter

doc = Formatter.format.__doc__

相关问题 更多 >