为什么我不能将关键字参数传递给list.index()方法? [英] Why I cant pass keyword argument to list.index() method?

查看:37
本文介绍了为什么我不能将关键字参数传递给list.index()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在检查python中list.index()方法的文档,我看到的是:

I was checking the documentation of list.index() method in python, what I saw is :

>>> help(list().index)
Help on built-in function index:

index(value, start=0, stop=9223372036854775807, /) method of builtins.list
instance
Return first index of value.

Raises ValueError if the value is not present.

当我运行下面的代码时,我遇到了一些错误.

When I ran the code below gave me some error.

>>> l=[1,2,3,43,45,5,6,6]
>>> l.index(43,start=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: index() takes no keyword arguments

推荐答案

错误消息指出 index 不使用关键字参数,但您要提供一个start = 1的

The error message says that index takes no keyword arguments but you are providing one with start=1

代替: l.index(43,start = 1)使用: l.index(43,1)

作为解释,可以解释它:

As for explanation, this could explain it:

许多内置函数仅使用METH_VARARGS,这意味着它们不支持关键字参数."len"更简单,并使用选项METH_O,这意味着它将获得一个对象作为参数.这保持代码非常简单,并且可能对性能.

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.

这篇关于为什么我不能将关键字参数传递给list.index()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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