Python过程函数



我正在查看python文档,并找到了一个称为 process的函数。我只在其他功能的文档中看到了它,但据我所知,它本身并未记录。

以下是文档中使用的一些示例。

来自@typing.overload

@overload
def process(response: None) -> None:
    ...
@overload
def process(response: int) -> Tuple[int, str]:
    ...
@overload
def process(response: bytes) -> str:
    ...
def process(response):
    <actual implementation>

来自fileinput

import fileinput
for line in fileinput.input():
    process(line)

最后,来自 Match objects

match = re.search(pattern, string)
if match:
    process(match)

我对上一次用法特别感兴趣。我的问题是,此process功能是什么,文档的位置?

在这些情况下, process只是示例占位符函数的任意名称,用于演示所记录的事物的示例用途。您通常会看到变量和功能,例如fooprocessbardo_something。通常,如果它是一个通用命名函数/变量,不是要记录的特定内容,并且在其他地方未定义,则将是占位符。

例如,最后一个情况可以写得更清楚(和言语),例如:

match = re.search(pattern, string)
if match:
    # placeholder
    # here is where you can be sure `match` is not None
    # and you can use it in your code, for example:
    process(match)

什么是过程函数?

它是一个占位符,它不存在(除非您定义它)。

这相当于说# Your code goes here,但指示目标对象(linematch ...)将由用户使用。

示例

match = re.search(pattern, string)
if match:
    process(match)

这是指"这是匹配工作和应使用的方式,请使用匹配对象做任何您想要的任何事情"

最新更新