进入ipython3中,使用help(sorted)
就会得到下面的帮助,参数中有/
和*
分别是什么作用呢?
Help on built-in function sorted in module builtins:
sorted(iterable, /, *, key=None, reverse=False)
Return a new list containing all items from the iterable in ascending order.
A custom key function can be supplied to customize the sort order, and the
reverse flag can be set to request the result in descending order.
/
的作用是指在/
之前的只能用位置传参,*
后面的只能关键字传参。
在Python3.6.9中,函数定义中并不能有/
而在Python3.8.2及以上就可以
从上面的测试来看,在/
之前的参数只能用位置传参,*
后面的只能用关键字传参,而在/
和*
之间的既可以位置传参,也可以关键字传参。