什么是= - 1"在Python方法中的参数列表中?
在下一页上: subprocess.popen.popen.popen 在参数中popen()
的列表有一个“ pipesize = -1
”。由于它在几个地方弹出,因此似乎不是错字。
但是,到目前为止,这可能是我的好奇心。
“ = -1
”是什么意思?
On the following page: subprocess.popen, in the argument list of popen()
there is a "pipesize=- 1
". Since it pops up at a few places, it does not seem to be a typo.
As for what this may be, however, so far eluded my curiosity.
What does the "=- 1
" mean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是文档的错误。它应该看起来像
pipesize = -1
。但是,无论如何,这是一个参数的默认值。默认情况下,函数中的参数和参数
,函数签名中的所有参数都必须在调用函数时传递一个参数:
上面的代码将成功运行而无需任何错误。
x
是功能的必需参数f
,我将位置参数正确地传递给x
。如果我不将论点转到所需的参数怎么办?
当给出此代码时,它会导致一个名为
typeError
的例外:这是因为,如上所述,必须基于
x
必须根据f的当前功能签名传递
。x
现在指定可选参数
,我将在function
f
中为参数x
提供默认值:此代码也将成功运行,例如函数的首次实现
f
/代码>。但是,在这里,函数签名看起来不同,当我们称为
f
时,我们没有传递任何内容。x =无
作为参数”。默认值与非默认参数
很重要的是要注意,在所有必需的参数之后,必须在功能签名中出现具有默认值的参数:
f
的此实现将导致syntaxerror:non---默认参数遵循默认参数
。在所有非默认参数之后,必须在函数签名中指定默认参数,这是有道理的。如果python没有引起上述f
的句法函数签名的错误,我们可以意外地将参数传递给x
而不是y
当我们不想。That is likely an error with the documentation. It should look like
pipesize = -1
. But, regardless, it is a default value for an argument.Parameters and Arguments in Functions
By default, all parameters in a function signature MUST be passed an argument when you call the function:
The code above will successfully run without any errors.
x
is a required parameter of functionf
, and I correctly passed a positional argument tox
.What if I don't pass an argument to a required parameter?
When given this code, it results in an exception called
TypeError
:This happens because, as stated above,
x
MUST be passed based on the current function signature off
.Specifying optional arguments
Now, I will provide a default value for parameter
x
in functionf
:This code will also successfully run, like the first implementation of function
f
. But, here the function signature looks different and we didn't pass anything when we calledf
.x=None
says that "ifx
isn't passed an argument in the function call tof
, it should be passedNone
as an argument".Default vs. Non-Default Parameters
It is important to note that parameters with defaults MUST appear, in the function signature, after ALL required parameters:
This implementation of
f
will result inSyntaxError: non-default argument follows default argument
. It makes sense that default arguments would have to be specified, in the function signature, after ALL non-default arguments. If Python didn't raise an error for the syntactically incorrect function signature off
above, we could accidentally pass an argument tox
instead ofy
when we do not want to.