日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Python3中省略號(...)用法介紹_python

作者:fengbingchun ? 更新時間: 2023-05-07 編程語言

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解釋器中的默認輔助提示符(default secondary prompt):

3. 它用于訪問和切片(accessing and slicing)多維數組/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
# 當函數的參數類型允許為Any
def inject(get_next_item: Callable[..., str]) -> None:
    ...
 
def foo(x: ...) -> None:
    ...
 
# 當函數的返回類型為Any
class flow:
    def __understand__(self, name: str, value: ...) -> None:
        ...

5. 它可用作函數內部的pass語句:

# 5. used as Pass Statement inside Functions
# foo1 and foo2 styles are same
def foo1():
    pass
 
def foo2():
    ...

6. 它可用作默認參數值:

# 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

原文鏈接:https://blog.csdn.net/fengbingchun/article/details/125284883

欄目分類
最近更新