在 IDLE 中调用函数时如何创建自己的 "hint"?
How can I create my own "hint" when calling a function in IDLE?
我知道我的问题可能令人困惑,很难问一些你不知道它到底是什么的东西(我打算称之为 'hint' ),所以我很抱歉。话虽如此,这就是我的意思(白框中的文字):
我的问题是:
- 如何在调用函数时制作自己的提示(并自定义)文本?
- 到底是什么?
- 这个例子中的文字是什么意思?
事实上你说的文本框被称为提示是正确的,所以不需要引用。您看到的是类型提示。 Python 中的 typing
模块支持它。参考下面的例子:
def add(a: int, b: int) -> int:
return a + b
然后当您尝试调用该函数时,您会看到一个与您显示的文本框非常相似的文本框。如果您想了解更多信息,我鼓励您阅读 typing
模块 page。
也就是说,在大多数情况下,您不需要该类型提示,因为文档字符串("""关于我的函数的信息""")通常更受追捧。
IDLE 将弹出窗口称为 'calltip'。对于在 python 代码中使用 def 语句定义的模块,它在第一行显示签名,在第二行显示文档字符串的第一行。它们旨在为使用括号调用的任何类型的可调用文件正常工作。
如果您输入“(”,除非您输入的速度很快,否则该框会弹出。在它关闭后,要将其恢复,您可以将光标放在“(”和“)”之间,然后 select 编辑在顶部菜单和 'Show call tip',或键入菜单上显示的快捷键。有关更多信息,see the doc.
供日后参考:
- 如何在调用函数时制作自己的提示 [...]?
首先,有两种类型的“提示”,我称之为:
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.
还有一个 introduced in Python 3.0:
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 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'
- 具体是什么?
根据上面写的和回答的内容:
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[=43= 的语法]是。
我知道我的问题可能令人困惑,很难问一些你不知道它到底是什么的东西(我打算称之为 'hint' ),所以我很抱歉。话虽如此,这就是我的意思(白框中的文字):
我的问题是:
- 如何在调用函数时制作自己的提示(并自定义)文本?
- 到底是什么?
- 这个例子中的文字是什么意思?
事实上你说的文本框被称为提示是正确的,所以不需要引用。您看到的是类型提示。 Python 中的 typing
模块支持它。参考下面的例子:
def add(a: int, b: int) -> int:
return a + b
然后当您尝试调用该函数时,您会看到一个与您显示的文本框非常相似的文本框。如果您想了解更多信息,我鼓励您阅读 typing
模块 page。
也就是说,在大多数情况下,您不需要该类型提示,因为文档字符串("""关于我的函数的信息""")通常更受追捧。
IDLE 将弹出窗口称为 'calltip'。对于在 python 代码中使用 def 语句定义的模块,它在第一行显示签名,在第二行显示文档字符串的第一行。它们旨在为使用括号调用的任何类型的可调用文件正常工作。
如果您输入“(”,除非您输入的速度很快,否则该框会弹出。在它关闭后,要将其恢复,您可以将光标放在“(”和“)”之间,然后 select 编辑在顶部菜单和 'Show call tip',或键入菜单上显示的快捷键。有关更多信息,see the doc.
供日后参考:
- 如何在调用函数时制作自己的提示 [...]?
首先,有两种类型的“提示”,我称之为:
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.
还有一个 introduced in Python 3.0:
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 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'
- 具体是什么?
根据上面写的和
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[=43= 的语法]是。