Why I cant pass keyword argument to list.index() method?

岁酱吖の 提交于 2021-02-16 20:33:33

问题


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

回答1:


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

Instead of: l.index(43, start=1) use: l.index(43, 1)

As for explanation, this could explain it:

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.




回答2:


The documentation has been poor on positional only parameters before, but on modern Python, they're improving. The key info is the seemingly out of place / in the signature:

index(value, start=0, stop=9223372036854775807, /)
                                                ^ This is not a typo!

That means that all arguments prior to the forward slash are positional only, they can't be passed by keyword. Per the Programming FAQ for "What does the slash(/) in the parameter list of a function mean?":

A slash in the argument list of a function denotes that the parameters prior to it are positional-only. Positional-only parameters are the ones without an externally-usable name. Upon calling a function that accepts positional-only parameters, arguments are mapped to parameters based solely on their position.




回答3:


index(value, start=0, stop=9223372036854775807, /)

document says here meaning of words is nothing but for what purpose you passing those arguments. for better understanding and how those arguments works, see below code example - list1 is list of 10 elements and index function is taking 3 arguments as starts and stops arguments are optional and for the purpose of slicing list, index function documentation explains it clear here -

list1 = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
for x in list1:
    print(list1.index(x, 0, 5))

output

0 7                                                                                                                     
1 1                                                                                                                     
2 4                                                                                                                     
3 9                                                                                                                     
4 16                                                                                                                    
Traceback (most recent call last):                                                                                      
  File "main.py", line 13, in <module>                                                                                  
    print(list1.index(x, 0, 5), x)                                                                                      
ValueError: 25 is not in list

code starts looking for elements from start point i.e 0 and stop point i.e 5 but for loop goes ahead of 5th index and raise the value error exception



来源:https://stackoverflow.com/questions/57235761/why-i-cant-pass-keyword-argument-to-list-index-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!