2 dongfuguo dongfuguo 于 2017.01.02 11:03 提问

Python函数参数列表最后的斜线是干啥的?

很多Python函数或者类的方法中,帮助文档都显示最后有一个斜线,但是自己定义函数时却不允许这样用,请问这个斜线的作用是什么呢?

help(sum)
Help on built-in function sum in module builtins:

sum(iterable, start=0, /)
Return the sum of a 'start' value (default: 0) plus an iterable of numbers

When the iterable is empty, return the start value.
This function is intended specifically for use with numeric values and may
reject non-numeric types.
>>> help(id)

Help on built-in function id in module builtins:

id(obj, /)
Return the identity of an object.

This is guaranteed to be unique among simultaneously existing objects.
(CPython uses the object's memory address.)

help(input)
Help on built-in function input in module builtins:

input(prompt=None, /)
Read a string from standard input. The trailing newline is stripped.

The prompt string, if given, is printed to standard output without a
trailing newline before reading input.

If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
On *nix systems, readline is used if available.
>>> help(filter)

Help on class filter in module builtins:

class filter(object)
| filter(function or None, iterable) --> filter object
|

| Return an iterator yielding those items of iterable for which function(item)
| is true. If function is None, return the items that are true.
|

| Methods defined here:
|

| getattribute(self, name, /)
| Return getattr(self, name).
|

| iter(self, /)
| Implement iter(self).
|

| new(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
|

| next(self, /)
| Implement next(self).
|

| reduce(...)
| Return state information for pickling.

2个回答

caozhy
caozhy   Ds   Rxr 2017.01.02 18:47
已采纳

可变参数个数,后面可以跟任意个数的数字

dongfuguo
dongfuguo 点错了,你这个答案不对,详见我后面追加的答案。
12 个月之前 回复
dongfuguo
dongfuguo   2017.01.02 21:32

已查找到正确答案,函数参数最后一个斜线表示该函数只接收位置参数而不接收关键参数,但是在Python中并不允许定义这样的函数,这样的函数一般是用C语言开发的内置函数或特定对象的方法,更加详细的资料可以查阅“Argument Clinic”有关资料。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!