如何创建自己的“提示"在IDLE中调用函数时?

如何创建自己的“提示

问题描述:

我知道我的问题可能令人困惑,很难问一些你不知道它到底是什么的东西(我将其称为'提示'),因此我对此表示歉意.话虽如此,这就是我的意思(白框中的文本):

I know my question might be confusing, it's hard to ask something you don't know what it is exactly (I'm going to call it 'hint'), so my apologies for that. Having that said, here it is what I mean (the text in the white box):

我的问题是:

  • 在调用函数时如何制作自己的提示文本(并对其进行自定义)?
  • 确切是什么?
  • 此示例中的文字是什么意思?

供以后参考:

  • 在调用函数时如何提出自己的提示[...]?

首先,我称之为"提示"有两种类型:

First, there are 2 types of 'hints' as I called them:

4.7.6.文档字符串:
以下是有关文档字符串的内容和格式的一些约定.

4.7.6. Documentation Strings:
Here are some conventions about the content and formatting of documentation strings.

第一行应始终是对象用途的简短摘要. [...] 此行应以大写字母开头,以句点结束.

The first line should always be a short, concise summary of the object’s purpose. [...] This line should begin with a capital letter and end with a period.

如果文档字符串中还有更多行,则第二行应为空白,以可视方式将摘要与描述的其余部分区分开.以下几行应为一个或多个描述对象的调用约定,其副作用等的段落. [...]

If there are more lines in the documentation string, the second line should be blank, visually separating the summary from the rest of the description. The following lines should be one or more paragraphs describing the object’s calling conventions, its side effects, etc. [...]

以下是多行文档字符串的示例:

Here is an example of a multi-line docstring:

>>> def my_function():
...     """Do nothing, but document it.
...
...     No, really, it doesn't do anything.
...     """
...     pass
...
>>> print(my_function.__doc__)
Do nothing, but document it.

    No, really, it doesn't do anything.

还有一个 Python 3.0引入的:

4.7.7.功能注释: 函数注释是有关所使用类型的完全可选的元数据信息通过用户定义的功能(有关更多信息,请参阅PEP 3107和PEP 484).

4.7.7. Function Annotations: Function annotations are completely optional metadata information about the types used by user-defined functions (see PEP 3107 and PEP 484 for more information).

注释作为字典存储在函数的 annotations 属性中,对函数的任何其他部分均没有影响.参数注释由参数名称后的冒号定义,后跟一个评估注释值的表达式.返回注释的定义是在参数列表和冒号(表示def语句的末尾)之间用文字->,然后是表达式.以下示例具有位置参数,关键字参数和带注释的返回值:

Annotations are stored in the annotations attribute of the function as a dictionary and have no effect on any other part of the function. Parameter annotations are defined by a colon after the parameter name, followed by an expression evaluating to the value of the annotation. Return annotations are defined by a literal ->, followed by an expression, between the parameter list and the colon denoting the end of the def statement. The following example has a positional argument, a keyword argument, and the return value annotated:

>>> def f(ham: str, eggs: str = 'eggs') -> str:
...     print("Annotations:", f.__annotations__)
...     print("Arguments:", ham, eggs)
...     return ham + ' and ' + eggs
...
>>> f('spam')
Annotations: {'ham': <class 'str'>, 'return': <class 'str'>, 'eggs': <class 'str'>}
Arguments: spam eggs
'spam and eggs'


  • 确切是什么?
  • 使用上面写的内容和

    With what is written above and what Terry Jan Reedy answered:

    IDLE将弹出窗口称为"calltip".对于用def语句在python代码中定义的模块,它在第一行显示签名,在第二行显示doc字符串的第一行.它们旨在用于使用括号调用的任何类型的callable都能正常工作.

    IDLE calls the popup a 'calltip'. For a module defined in python code with a def statement, it shows the signature on the first line and the first line of the doc string on the second. They are intended to work correctly for any type of callable invoked with parentheses.

    如果您键入'(',除非您快速键入,否则将弹出该框.将其关闭后,将其放回原处,请将光标置于'('和')'之间,然后选择顶部菜单上的编辑"和显示呼叫提示",或键入菜单上显示的快捷键.有关更多信息,请参见文档.

    If you type '(' the box pops up unless you are typing fast. After it closes, to bring it back, you position the cursor between '(' and ')' and either select Edit on the top menu and 'Show call tip', or type the shortcut key shown on the menu. For more, see the doc.


    • 此示例中的文字是什么意思?
    • 可悲的是,由于我现在不忙于我的工作,并且我从来不需要了解它,所以我没有发现每个 calltip 的语法是什么.

      Sadly, because I'm not working on with what I was at the moment and I never had the need to understand it, I have not found what the syntax for each calltip is.