Python3 以后的特性 ...只是占位?

返回值 None

def test(): ...

返回值 None

def test(): pass

返回值都一样,这个三个点和 pass 区别在哪呢?都是占位的吗?


Python3 以后的特性 ...只是占位?
5 回复

Ellipsis 对象


我无法理解你的问题。

以后遇到这种疑问,可以先去查一下 ... 的含义:
https://docs.python.org/3/library/constants.html#Ellipsis

... 是个常量对象,在你这种场景下,换成 123 效果也一样。

$ python3 -c 'a = …; print(a)'
Ellipsis

$ python3 -c 'help(…)'
Help on ellipsis object:

class ellipsis(object)
| Methods defined here:
|
| getattribute(self, name, /)
| Return getattr(self, name).
|
| reduce(…)
| Helper for pickle.
|
| repr(self, /)
| Return repr(self).
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| new(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.

…是个特别的量,第一个例子里面换成随便 123 或者"123"都一样,函数里面没有返回值所以返回了 None



感谢,刚刚去看过文档没看到那块所以才提问的。

回到顶部