python切片的物件 Python切片物件和

2021-10-18 14:54:16 字數 1060 閱讀 6372

python中是否存在某種內部機制,它以不同的方式處理傳遞給__getitem_

_的引數,並自動將start:stop:step構造轉換為片?在

這是我的意思class exampleclass(object):

def __getitem__(self, *args):

return args

def __call__(self, *args):

return args

def randommethod(self, *args):

return args

a = exampleclass()

#this works

print a[3:7:2, 1:11:2]

#syntax error on the first colon

print a.randommethod(3:7:2, 1:11:2)

print a(3:7:2, 1:11:2)

#these work

print a.randommethod(slice(3,7,2), slice(1,11,2))

print a(slice(3,7,2), slice(1,11,2))

直譯器是否只是在內搜尋start:stop:step的例項,並將它們交換為slice(start, stop, step)?檔案只簡單地說:the bracket (subscript) notation uses slice objects internally

這是python內部的乙個部分,我不能改變它的行為嗎?是否可以讓其他函式使用start:stop:step速記獲取切片物件?*在

*我已經看到了另乙個問題,can python's slice notation be used outside of brackets?,但這只是使用乙個自定義類,我可以很容易地做到這一點。我想要的是一種只使用start:stop:step而不必將其包裝在其他任何東西中的方法。在

旁註:它還表示[...]中的所有引數都打包成tuple,有點像它在做[*args]->;__getitem__(args)。在

^$

python物件怎麼理解 如何理解python物件

類 class 抽象的概念,比如說人類 鳥類 水果 是乙個總的稱呼,沒有具體到某個物體 物件 object,指具體例項,instance 給物件增加乙個例項變數 增加乙個skills例項變數 p.skills programming writing print p.skills 刪除p物件的name...

python 切片 Python 列表切片

想必很多人都使用過列表的切片,通過切片可以從列表中獲取乙個或多個元素,但你真的了解切片?一 一般玩法 name a b c d e f g h name 0 2 獲取 0 2 中間的元素,不包括索引為 2 的元素 a b name 2 從 0 開始切,可省略 0 a b name 1 同樣地,切到最...

python切片的作用 Python切片索引用法

這篇文章主要介紹了python切片索引用法,結合例項形式詳細分析了python切片索引的常見使用方法與操作注意事項,需要的朋友可以參考下 在python中,可以用用簡單的方括號加乙個下標的方式訪問序列的每乙個元素,這種方式稱之為切片操作符,切片操作符有三種形式 訪問某一資料元素的語法如下 seque...