【python】類內置函數

python類內置函數:

# __init__:類構造函數
# __slots__:允許類添加的屬性名
# __str__:類似java類toString方法
# __repr__:引用對象toString
# __iter__:for...in,類似java.List的iterator,以StopIteration()結束循環
# __getitem__:類似java.ArrayList的下標索引或者java.Map的key索引,但需要支持切片,步長遞進
# __setitem__:與__getitem__對應
# __delitem__:與__getitem__對應
# __getattr__:返回對象指定屬性值, __setattr__,...

1、__getitem__

TODO:嘗試動態變長的list,具有list的下標索引、切片特性,類似於下面的結果:

索引號 0 1 2 3 4
索引號 -5 -4 -3 -2 -1
0 1 2 3 4
那麼,對上面list=[0,1,2,3,4]取值,有
list[::]=[0,1,2,3,4]
list[0:30]=[0,1,2,3,4]
list[1:4:0] # error

list[0:4:2]=[0,2]
list[4:0:-2]=[4,2]
list[0:4:-2]=[]
list[4:0:2]=[]

list[-4:-2]=[1,2]
list[-2:-5:-1]=[3,2,1]
list[-4:-2:1]=[1,2]
list[-2:-5]=[]
list[-4:-2:-1]=[]

list[-5:4:2]=list[0:4:2]=[0,2]
list[-5:30]=list[0:5]=[0,1,2,3,4]
list[3:-1]=list[3:4]=[3]
......

實現:

class Fib(object):
    def __init__(self, limit=100):
        self.__limit = limit

    def __getitem__(self, item):
        limit = self.__limit
        if isinstance(item, int): # 下標索引
            a, b = 0, 1
            if item >= 0:
                if item > limit:
                    raise AttributeError('out bound of list size')
                end = item+1
            else:
                if limit+item < 0:
                    raise AttributeError('out bound of list size')
                end = limit + item + 1
            for i in range(end):
                a, b = b, a+b
            return a
        if isinstance(item, slice):
            start = 0 if item.start is None else item.start
            end = limit if item.stop is None else item.stop
            step = 1 if item.step is None else item.step

            if step == 0:
                raise AttributeError('slice step cannot be zero')

            L = []
            if (start>=0 and end<0) or (end>=0 and start<0):
                start = start if start>0 else (0 if start<-limit else limit+start)
                end = end if end>0 else (0 if end<-limit else limit+end)

            if start >= 0 and end >= 0:
                if start > end and step < 0:
                    order = 1 # reverse
                    step = -step
                    start, end = end + step, start + step
                elif start < end and step > 0:
                    order = 0
                else: # start < end and step < 0
                    return L
            elif start < 0 and end < 0:
                if start < end and step > 0:
                    order = 0
                    start, end = limit+start+1, limit+end+1
                elif start > end and step < 0:
                    order = 1
                    step = -step
                    start, end = limit + end + step, limit + start + step
                else:
                    return L
            else:
                print "1.???"
                return

            temp = start

            end = limit if end>limit else end
            a, b = 0, 1
            for i in range(end):
                a, b = b, a+b
                if i >= temp:
                    L.append(a)
                    temp = temp + step
            if order == 0:
                return L
            else:
                L.reverse()
                return L




python類內置函數很多,後面學到了再往這裏添吧。

發佈了55 篇原創文章 · 獲贊 10 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章