加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

如何为所有类方法添加常量字符串到__doc__?

发布时间:2020-12-20 11:31:54 所属栏目:Python 来源:网络整理
导读:参见英文答案 How do I programmatically set the docstring?????????????????????????????????????6个 我的代码看起来像这样: constString = """Default docstring info: 1 2 3"""class A(): def A1(): """ First unique docstring. """ pass def A2(): ""
参见英文答案 > How do I programmatically set the docstring?????????????????????????????????????6个
我的代码看起来像这样:

constString = """
Default docstring info:
    1
    2
    3"""

class A():

    def A1():
        """
        First unique docstring.
        """
        pass

    def A2():
        """
        Second unique docstring.
        """
        pass
B = A()
print(B.A1.__doc__)

如果我运行此代码,我会重新输出:

First unique docstring.

Second unique docstring.

但是我想通过为类A中的所有方法添加constString来替换方法的docstring.输出必须如下所示:

First unique docstring.
Default docstring info:
1
2
3

Second unique docstring.
Default docstring info:
1
2
3

我怎么能这样做?

解决方法

instancemethod的docstring取自底层函数,这就是为什么B.A1 .__ doc__ = constString不起作用的原因.然而:

B.A1.__func__.__doc__ += constString

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读