作者:小K
来源:麦叔编程
查阅 Python 官方文档后可以看到,它是一个 「「内置常量」」 Built-in Constant)。经常用于对用户自定义的容器数据类型进行切片用法的扩展。
❝
Guido van Rossum 这么一位被人称为「仁慈的独裁者」的 Python 之父采纳 Ellipsis 的原因竟然是因为:「有人认为三个省略号的写法可爱」。(原文为:「Some folks thought it would be cute to be able to write incomplete code like this」)
❞
Type "help", "copyright", "credits" or "license" for more information.
>>> ...
Ellipsis
>>> type(...)
❝
...是ellipsisclass的一个单例(这个类唯一的一个实例。)
❞
def func1():
pass
def func2():
...
func1()
func2()
换种写法:
def fun3():...
官方文档说Ellipsis主要用于用户自定义容器类型的切片操作,但是实际运用中发现只有在Numpy中有用。
>>> import numpy as np
>>> arr = np.arange(9).reshape((3,3))
>>> arr
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> arr[...,1:2]
array([[1],
[4],
[7]])
>>> arr[2, ...]
array([6, 7, 8])
等价于
>>> arr[:,1:2]
array([[1],
[4],
[7]])
>>> arr[2,:]
array([6, 7, 8])
❝
只能在Numpy的高维数组中使用。
❞
如果你希望我更新某个特定小知识,欢迎给我留言。
留言与评论(共有 0 条评论) “” |