【问题标题】:Can I make IDLE shell show me the description of a function?我可以让 IDLE shell 显示函数的描述吗?
【发布时间】:2013-01-02 10:13:45
【问题描述】:

所以我是 Python 新手,我正在使用 IDLE。我使用什么命令,所以 Python shell(按 f5 后)向我显示了描述(我在函数名后面的“”“”“”中写的东西) 和/或显示其他功能的描述!?

如果它在 2.7 和 3.3 中有所不同,如果您提及,我将不胜感激。

【问题讨论】:

    标签: python python-3.x python-2.7


    【解决方案1】:

    你写在""""""里的东西叫做docstring。

    当你想打印它时,你可以使用类型的__doc__ 属性。 (类型表示类、方法或模块)。

    您可以使用内置函数dir检查可用属性

    【讨论】:

      【解决方案2】:

      “事物”被称为Docstring,可以通过其字典属性__ doc __轻松访问

      >>> def testfunc():
      ...     """ My Docstrings """
      ...     print "test"
      ... 
      
      >>> testfunc.__doc__
      ' My Docstrings '
      

      【讨论】:

        【解决方案3】:

        你使用:

        help(your_function_name)
        

        就像我在这里所做的:

        >>> def sayhello():
            """This says hello to you"""
            print "Hello there!"
        
        >>> help(sayhello)
        Help on function sayhello in module __main__:
        
        sayhello()
            This says hello to you
        

        编辑: 您必须先导入自己的模块。

        【讨论】:

        • 如果他需要一个字符串,这是行不通的,因为它会打印到 std.out 并且需要使用 pydoc 模块中的 render_doc 方法
        • @Samuele:我知道,但他问他应该使用什么命令向他显示描述。
        • 我知道 :) 我只是为了澄清他而添加 :)
        • 没问题:)。毕竟,新人必须学习他们能做到多少。
        【解决方案4】:

        使用 help() 函数

        示例:

        帮助(打印) 关于内置模块内置函数打印的帮助:

        打印(...) print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

        Prints the values to a stream, or to sys.stdout by default.
        Optional keyword arguments:
        file:  a file-like object (stream); defaults to the current sys.stdout.
        sep:   string inserted between values, default a space.
        end:   string appended after the last value, default a newline.
        flush: whether to forcibly flush the stream.
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-03-01
          • 2021-01-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-20
          相关资源
          最近更新 更多