什么是= - 1"在Python方法中的参数列表中?

发布于 2025-01-29 21:23:48 字数 324 浏览 2 评论 0原文

在下一页上: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

李白 2025-02-05 21:23:48

这可能是文档的错误。它应该看起来像pipesize = -1。但是,无论如何,这是一个参数的默认值。

默认情况下,函数中的参数和参数

,函数签名中的所有参数都必须在调用函数时传递一个参数:

def f(x):
    pass

f("argument for x")

上面的代码将成功运行而无需任何错误。 x是功能的必需参数f,我将位置参数正确地传递给x

如果我不将论点转到所需的参数怎么办?

当给出此代码时,它会导致一个名为typeError的例外:

def f(x):
    pass

f() # TypeError: f() missing 1 required positional argument: 'x'

这是因为,如上所述,必须基于x必须根据f的当前功能签名传递x

现在指定可选参数

,我将在function f中为参数x提供默认值:

def f(x=None):
    pass

f()

此代码也将成功运行,例如函数的首次实现f /代码>。但是,在这里,函数签名看起来不同,当我们称为f时,我们没有传递任何内容。 x =无作为参数”。

默认值与非默认参数

很重要的是要注意,在所有必需的参数之后,必须在功能签名中出现具有默认值的参数:

def f(x=None, y):
    pass

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:

def f(x):
    pass

f("argument for x")

The code above will successfully run without any errors. x is a required parameter of function f, and I correctly passed a positional argument to x.

What if I don't pass an argument to a required parameter?

When given this code, it results in an exception called TypeError:

def f(x):
    pass

f() # TypeError: f() missing 1 required positional argument: 'x'

This happens because, as stated above, x MUST be passed based on the current function signature of f.

Specifying optional arguments

Now, I will provide a default value for parameter x in function f:

def f(x=None):
    pass

f()

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 called f. x=None says that "if x isn't passed an argument in the function call to f, it should be passed None 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:

def f(x=None, y):
    pass

This implementation of f will result in SyntaxError: 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 of f above, we could accidentally pass an argument to x instead of y when we do not want to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文