默认参数和关键字参数之间的区别 [英] Difference between default arguments and keyword arguments

查看:205
本文介绍了默认参数和关键字参数之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在函数教程中有关于默认参数和

关键字参数的部分,但我没有看到它们之间的语法差异。

对于默认参数教程显示:


def ask_ok(提示,重试= 4,投诉=''是或否,请!''):


对于关键字参数,教程显示:

def parrot(电压,状态=''僵硬'',动作=''voom'',type =''Norwegian Blue' '):


就我所见,语法''keyword = value''用于两者。

如何区分它们或者它们都是同一组合

概念的一部分,即:如果一个人调用的函数少于所需的

参数个数但指定关键字值,使用这些值,

否则提供默认值。或者我在上面错过了默认参数和关键字参数之间是否存在语法差异?

In the tutorial on functions there are sections on default arguments and
keyword arguments, yet I don''t see the syntactic difference between them.
For default arguments the tutorial shows:

def ask_ok(prompt, retries=4, complaint=''Yes or no, please!''):

while for keyword arguments the tutorial shows:

def parrot(voltage, state=''a stiff'', action=''voom'', type=''Norwegian Blue''):

The syntax ''keyword = value'' is used for both as far as I can see. How does
one distinguish between them or are they both part of the same combined
concept, which is: if one calls the function with less than the required
number of arguments but does specify keyword values, those values are used,
else the defaults are supplied. Or is there really a syntactic difference
between default arguments and keyword arguments which I have missed above ?

推荐答案

Edward Diener写道:
Edward Diener wrote:
在函数教程中有关于默认参数和
关键字参数的部分,但我没有看到它们之间的语法差异。
对于默认参数教程显示:

def ask_ok(提示,重试= 4,投诉=''是或否,请!''):

对于关键字参数,教程显示:

def parrot(电压,状态=''僵硬'',动作=''voom'',type =''Norwegian Blue''):

语法就我所见,''keyword = value''用于两者。如何区分它们或者它们都是同一组合
概念的一部分,即:如果一个函数调用的函数少于所需的参数数量但指定了关键字值,使用这些值,
否则提供默认值。或者我在上面遗漏的默认参数和关键字参数之间是否存在真正的语法差异?
In the tutorial on functions there are sections on default arguments and
keyword arguments, yet I don''t see the syntactic difference between them.
For default arguments the tutorial shows:

def ask_ok(prompt, retries=4, complaint=''Yes or no, please!''):

while for keyword arguments the tutorial shows:

def parrot(voltage, state=''a stiff'', action=''voom'', type=''Norwegian Blue''):

The syntax ''keyword = value'' is used for both as far as I can see. How does
one distinguish between them or are they both part of the same combined
concept, which is: if one calls the function with less than the required
number of arguments but does specify keyword values, those values are used,
else the defaults are supplied. Or is there really a syntactic difference
between default arguments and keyword arguments which I have missed above ?



所有参数都是关键字参数。它们可能有也可能没有默认值

值。在您的示例中,voltage是关键字参数,但它没有默认的



请考虑以下内容:


All arguments are keyword arguments. They may or may not have a default
value. In your example, voltage is a keyword argument, but it has no
default.

Consider the following:

def fn(a,b):
print''a ='',a

print''b ='' ,b


fn(b = 1,a = 2)
a = 2

b = 1 fn(1,2)
a = 1

b = 2
def fn(a,b): print ''a = '',a
print ''b = '',b

fn(b=1,a=2) a = 2
b = 1 fn(1,2) a = 1
b = 2



希望这会有所帮助,

Mike


Hope this helps,
Mike


DoubleM写道:
DoubleM wrote:
Edward Diener写道:
Edward Diener wrote:
在函数教程中有关于默认参数的部分
和关键字争论,但我没有看到他们之间的句法差异。对于默认参数,本教程显示:

def ask_ok(提示,重试= 4,投诉=''是或否,请!''):

对于关键字参数教程显示:

def parrot(电压,状态=''僵硬'',动作=''voom'',type =''Norwegian
Blue''):

语法''keyword = value''用于我可以看到的两个。
如何区分它们或者它们都是
相同组合概念的一部分,即:如果一个函数调用的函数少于所需的参数数量但确实指定了关键字值,则使用这些值,否则提供默认值。或者上面我错过了默认参数和
关键字参数之间的语法差异吗?
In the tutorial on functions there are sections on default arguments
and keyword arguments, yet I don''t see the syntactic difference
between them. For default arguments the tutorial shows:

def ask_ok(prompt, retries=4, complaint=''Yes or no, please!''):

while for keyword arguments the tutorial shows:

def parrot(voltage, state=''a stiff'', action=''voom'', type=''Norwegian
Blue''):

The syntax ''keyword = value'' is used for both as far as I can see.
How does one distinguish between them or are they both part of the
same combined concept, which is: if one calls the function with less
than the required number of arguments but does specify keyword
values, those values are used, else the defaults are supplied. Or is
there really a syntactic difference between default arguments and
keyword arguments which I have missed above ?


所有参数都是关键字参数。它们可能具有或不具有
默认值。在您的示例中,voltage是关键字参数,但它没有默认值。

请考虑以下内容:


All arguments are keyword arguments. They may or may not have a
default value. In your example, voltage is a keyword argument, but
it has no default.

Consider the following:

> >> def fn(a,b):print''a ='',a
print''b ='',b

>>> fn(b = 1,a = 2)a = 2
b = 1>>> fn(1,2)
>>> def fn(a,b): print ''a = '',a
print ''b = '',b

>>> fn(b=1,a=2) a = 2
b = 1 >>> fn(1,2)


a = 1
b = 2


a = 1
b = 2




有道理。谢谢 !本教程应该更清楚地解释它。



Makes sense. Thanks ! The tutorial should explain it more clearly.


2004-04-04,DoubleM< Do ******* @ netscape.net>写道:
On 2004-04-04, DoubleM <Do*******@netscape.net> wrote:
所有参数都是关键字参数。它们可能有也可能没有默认值。在你的例子中,电压是一个关键字参数,但它没有默认值。
All arguments are keyword arguments. They may or may not have a default
value. In your example, voltage is a keyword argument, but it has no
default.




但这是什么意思?:



But what does this mean?:

__import __(name =" eggs")
__import__(name="eggs")



回溯(最近一次调用最后一次):

文件"< stdin>",第1行,在?

TypeError:__ import __()不带关键字参数

是因为其他参数是可选的吗?该函数定义如下:
如下:


__import __(name [,globals [,locals [,fromlist]]])

Marco

-

Marco Herrn 他*** @ gmx。 net

(首选GnuPG / PGP签名和加密邮件)

密钥ID:0x94620736


Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: __import__() takes no keyword arguments
Is it because the other arguments are optional? The function is defined
as follows:

__import__( name[, globals[, locals[, fromlist]]])

Marco
--
Marco Herrn he***@gmx.net
(GnuPG/PGP-signed and crypted mail preferred)
Key ID: 0x94620736


这篇关于默认参数和关键字参数之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆