Python3中省略號(...)用法介紹
1. 省略號(...)是一個Python對象,叫Ellipsis,它沒有方法,是一個單例對象(singleton object):
# 1. ...是一個python對象,叫Ellipsis print(type(...)) # output: <class 'ellipsis'> print(...) # output: Ellipsis print(Ellipsis) # output: Ellipsis print(bool(...)) # output: True
2. 它可用作Python解釋器中的默認(rèn)輔助提示符(default secondary prompt):

3. 它用于訪問和切片(accessing and slicing)多維數(shù)組/NumPy索引,注:不能在一個切片中有多個省略號
# 3. slice: we can not have multiple ellipsis in a single slicing array = np.random.rand(2, 4) # a 2-dimensional matrix of order 2*4(rows*cols) print(array); print(array[...]); print(array[Ellipsis]) # they are all equivalent print(array[..., 0]); print(array[:,0]); print(array[Ellipsis, 0]) # they are all equivalent print(array[0, ...])
4. 它可用在類型提示中(in type hinting):
# 4. type hints
# 當(dāng)函數(shù)的參數(shù)類型允許為Any
def inject(get_next_item: Callable[..., str]) -> None:
...
def foo(x: ...) -> None:
...
# 當(dāng)函數(shù)的返回類型為Any
class flow:
def __understand__(self, name: str, value: ...) -> None:
...5. 它可用作函數(shù)內(nèi)部的pass語句:
# 5. used as Pass Statement inside Functions
# foo1 and foo2 styles are same
def foo1():
pass
def foo2():
...6. 它可用作默認(rèn)參數(shù)值:
# 6. used as a default argument value
def foo3(x = ...):
return x
def foo4(x = None):
return x
print("foo3:", foo3) # output: foo3: <function foo3 at 0x7f4e7ffcf5e0>
print("foo4:", foo4) # output: foo4: <function foo4 at 0x7f4e7ffcf550>GitHub:https://github.com/fengbingchun/Python_Test
到此這篇關(guān)于Python3中省略號(...)用法介紹的文章就介紹到這了,更多相關(guān)Python3 省略號內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
前女友發(fā)來加密的"520快樂.pdf",我用python破解開之后,卻發(fā)現(xiàn)
520收到前女友發(fā)來的加密PDF文件,說打開之后有驚喜,難道是要復(fù)合?我用python破解開之后,卻發(fā)現(xiàn)...python干貨+劇情滿滿收藏收藏2021-08-08
Python模塊psycopg2連接postgresql的實現(xiàn)
本文主要介紹了Python模塊psycopg2連接postgresql的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Python實現(xiàn)的北京積分落戶數(shù)據(jù)分析示例
這篇文章主要介紹了Python實現(xiàn)的北京積分落戶數(shù)據(jù)分析,結(jié)合實例形式分析了Python針對北京積分落戶數(shù)據(jù)的分析、運(yùn)算、展示等相關(guān)操作技巧,需要的朋友可以參考下2020-03-03
利用插件和python實現(xiàn)Excel轉(zhuǎn)json的兩種辦法
轉(zhuǎn)換Excel表格到JSON格式有很多方法,下面這篇文章主要給大家介紹了關(guān)于利用插件和python實現(xiàn)Excel轉(zhuǎn)json的兩種辦法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11
詳解Python的Django框架中inclusion_tag的使用
這篇文章主要介紹了詳解Python的Django框架中inclusion_tag的使用,文中示例基于Python較早的2.x版本,希望能夠注意一下,需要的朋友可以參考下2015-07-07

