简体   繁体   English

Python 文档:urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None 中的方括号是什么意思?

[英]Python doc: what do the square brackets mean in urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None?

I am learning python and came across this description of a function in urllib.request in the documentation of the standard library:我正在学习 python 并在标准库的文档中的 urllib.request 中遇到了 function 的描述:

> urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None,
> capath=None, cadefault=False, context=None)

I couldn't figure out what the square brackets around timeout, mean.我无法弄清楚timeout,周围的方括号是什么意思。

I am not even sure how to read the list of parameters of this function: do we have a parameter [timeout and then a parameter ]* separated by a comma, or is [timeout, ]* a parameter as a whole?我什至不知道如何阅读这个 function 的参数列表:我们是否有一个参数[timeout ,然后是一个参数]* ,用逗号分隔,还是[timeout, ]*是一个整体的参数?

In which case, what does the comma after timeout inside the square brackets mean?在这种情况下,方括号内超时后的逗号是什么意思?

Also, what does the star/asterisk mean in this particular case?另外,在这种特殊情况下,星号/星号是什么意思?

Thanks !谢谢 !

Square brackets in parameter documentation generally denote an optional parameter.参数文档中的方括号通常表示可选参数。 This not only the case in Python documentation, but is actually a widely-used convention.这不仅是 Python 文档中的情况,而且实际上是一种广泛使用的约定。

So, in this case, it means that timeout is optional, which is confirmed by the documentation text ( bold emphasis mine):因此,在这种情况下,这意味着timeout是可选的, 文档文本粗体强调我的)证实了这一点:

The optional timeout parameter specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout setting will be used).可选timeout参数以秒为单位指定连接尝试等阻塞操作的超时时间(如果未指定,将使用全局默认超时设置)。 This actually only works for HTTP, HTTPS and FTP connections.这实际上仅适用于 HTTP、HTTPS 和 FTP 连接。

An asterisk in parameter documentation usually means "zero or more repetitions".参数文档中的星号通常表示“零次或多次重复”。 However, the Python documentation actually uses ellipsis ( ) for that meaning.但是,Python 文档实际上使用省略号 ( ) 来表示该含义。

In fact, this asterisk is not actually part of the documentation at all, it is just normal function parameter declaration syntax.事实上,这个星号实际上根本不是文档的一部分,它只是普通的 function 参数声明语法。

It essentially means "end of positional arguments, everything after this must be passed as a keyword argument".它本质上意味着“位置 arguments 的结束,此后的所有内容都必须作为关键字参数传递”。

See Bare asterisk in function arguments?请参阅function arguments 中的裸星号? and Forced naming of parameters in Python for more details and PEP 3102 – Keyword-Only Arguments for the specification.在 Python 中强制命名参数以获取更多详细信息,并在PEP 3102 – 仅关键字 Arguments中获取规范。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM