内置函数和关键字参数 [英] Built-in functions and keyword arguments

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

问题描述

当我尝试执行此操作时,为什么Python会出错:

Why does Python give an error when I try to do this:


> > len(object = [1,2])
>>len(object=[1,2])



Traceback(最近一次调用最后一次):

文件"< pyshell#40>",第1行,< module>

len(object = [1,2])

TypeError: len()不使用关键字参数


但是当我使用正常时不会功能:

Traceback (most recent call last):
File "<pyshell#40>", line 1, in <module>
len(object=[1,2])
TypeError: len() takes no keyword arguments

but not when I use a "normal" function:


>> def my_len(object):
>>def my_len(object):



返回len(对象)

return len(object)


>> my_len(object = [1,2])
>>my_len(object=[1,2])



2

2

推荐答案

Armando Serrano Lombillo< ar ****** @ gmail.comwrote:
Armando Serrano Lombillo <ar******@gmail.comwrote:

为什么我尝试这样做时Python会出错:
Why does Python give an error when I try to do this:

>>> len(object = [ 1,2])
>>>len(object=[1,2])



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

文件"< pyshell#40> ",第1行,在< module>

len(object = [1,2])

TypeError:len()不带关键字a
但不是我使用正常的时候功能:

Traceback (most recent call last):
File "<pyshell#40>", line 1, in <module>
len(object=[1,2])
TypeError: len() takes no keyword arguments

but not when I use a "normal" function:


>>> def my_len(object):
>>>def my_len(object):



返回len(对象)

return len(object)


>>> my_len(object = [1, 2])
>>>my_len(object=[1,2])



2

2



在C级,有几种选择如何你定义了一个可以从Python调用的函数
。最通用的形式是

METH_VARARGS | METH_KEYWORDS,它接受参数元组和关键字参数的

字典。然后必须解析这些实际参数。


许多内置函数只使用METH_VARARGS,这意味着它们是
不支持关键字参数。 " LEN"甚至更简单,并使用一个

选项METH_O,这意味着它获得一个对象作为参数。这个

保持代码非常简单,也可以对

性能略有不同。


我不知道如果大多数内置函数不接受

关键字的原因只是历史性的(有人必须经历很多

代码并添加关键字参数名称)或者如果没有使用METH_KEYWORDS选项确实存在任何可衡量的性能差异。

能够编写更少的C代码可能是主要因素。

At the C level there are several options for how you define a function
callable from Python. The most general form is
METH_VARARGS|METH_KEYWORDS which accepts both a tuple of arguments and a
dictionary of keyword arguments. These then have to be parsed to find
the actual arguments.

Many of the builtin functions use only METH_VARARGS which means they
don''t support keyword arguments. "len" is even simpler and uses an
option METH_O which means it gets a single object as an argument. This
keeps the code very simple and may also make a slight difference to
performance.

I don''t know if the reason that most builtin functions don''t accept
keywords is just historical (someone would have to go through a lot of
code and add keyword argument names) or if there really is any
measurable performance difference to not using the METH_KEYWORDS option.
Being able to write less C code may be the main factor.


Armando Serrano Lombilloaécrit:
Armando Serrano Lombillo a écrit :

为什么当我尝试这样做时Python会出错:
Why does Python give an error when I try to do this:

>>> len(object = [1,2])
>>>len(object=[1,2])



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

文件"< pyshell#40>",第1行,< module>

len( object = [1,2])

TypeE rror:len()不使用关键字参数


但是当我使用正常时功能:

Traceback (most recent call last):
File "<pyshell#40>", line 1, in <module>
len(object=[1,2])
TypeError: len() takes no keyword arguments

but not when I use a "normal" function:


>>> def my_len(object):
>>>def my_len(object):



返回len(对象)

return len(object)


>>> my_len(object = [1, 2])
>>>my_len(object=[1,2])



2

2



在第二种情况下,参数的名称*是*''对象''。对于内置len(其中,fwiw,类型为
''builtin_function_or_method'',而不是''function''的情况,这不是

,所以inspect.getargspec < br $>
不能告诉我更多信息。


< ot>

虽然我们在这里,你应该避免在

标识符中使用内置的名称 - 这里,使用''object'作为arg名称隐藏内置的

''对象''类)。 br />
< / ot>

In the second case, the name of the argument *is* ''object''. Which is not
the case for the builtin len (which, fwiw, has type
''builtin_function_or_method'', not ''function'', so inspect.getargspec
couldn''t tell me more).

<ot>
While we''re at it, you should avoid using builtin''s names for
identifiers - here, using ''object'' as the arg name shadows the builtin
''object'' class).
</ot>


2007年10月29日星期一13:52:04 +0000,Armando Serrano Lombillo写道:
On Mon, 29 Oct 2007 13:52:04 +0000, Armando Serrano Lombillo wrote:

为什么Python尝试这样做时会出错:
Why does Python give an error when I try to do this:

>>> len(对象= [1,2])
>>>len(object=[1,2])



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

文件"< pyshell#40>",第1行,在< module>

len(object = [1,2])

TypeError:len()不使用关键字参数


但不是在我使用正常时功能:

Traceback (most recent call last):
File "<pyshell#40>", line 1, in <module>
len(object=[1,2])
TypeError: len() takes no keyword arguments

but not when I use a "normal" function:


>>> def my_len(object):
>>>def my_len(object):



返回len(对象)

return len(object)


>>> my_len(object = [1, 2])
>>>my_len(object=[1,2])



2

2



因为len()不带关键字参数,只需就像它说的那样,但my_len()

是这样写的,所以它需要一个关键字参数。


当你调用一个函数foo(object = [1, 2]你指示Python将
绑定到关键字参数object的值[1,2]。但是如果

函数没有名为object的关键字参数。然后它会立即失败

。由于len()没有任何关键字参数,自然它没有一个名为对象的



你可以看到这里的效果相同:

Because len() takes no keyword arguments, just like it says, but my_len()
is written so it DOES take a keyword argument.

When you call a function foo(object=[1,2]) you are instructing Python to
bind the value [1,2] to the keyword argument "object". But if the
function doesn''t have a keyword argument named "object" then it will fail
immediately. Since len() doesn''t have ANY keyword arguments, naturally it
doesn''t have one called "object".

You can see the same effect here:


>> def my_len2(* args):
>>def my_len2(*args):



....返回len(arg [0])

....

.... return len(arg[0])
....


>> my_len2(object = [1,2])
>>my_len2(object=[1,2])



Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,< module>

TypeError:my_len2()得到一个意外的关键字参数''object''

除了错误信息略有不同外,很多(大多数?全部?)

的内置函数就像my_len2()。


你可能会想到关键字参数相当于:


object = [1,2]

len(对象)


情况并非如此。它们完全不相同。


-

史蒂文。

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: my_len2() got an unexpected keyword argument ''object''
Apart from the error message being slightly different, many (most? all?)
of the built in functions are like my_len2().

You may be thinking that keyword arguments are the equivalent of this:

object=[1,2]
len(object)

That is not the case. They are not at all equivalent.

--
Steven.


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

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