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

2019-07-19 11:38:51 +08:00
 louhang

返回值 None

def test(): ...

返回值 None

def test(): pass

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

2139 次点击
所在节点    Python
4 条回复
blacklinux
2019-07-19 11:52:25 +08:00
Ellipsis 对象
siteshen
2019-07-19 11:55:00 +08:00
以后遇到这种疑问,可以先去查一下 `...` 的含义:
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.
Trim21
2019-07-19 11:57:28 +08:00
...是个特别的量,第一个例子里面换成随便 123 或者"123"都一样,函数里面没有返回值所以返回了 None
louhang
2019-07-19 12:03:31 +08:00
@blacklinux
@siteshen
@Trim21 感谢,刚刚去看过文档没看到那块所以才提问的。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/584354

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX