Discuss / Python / Fib
class Fib:
    def __getitem__(self, item):
        if isinstance(item, int):
            if item > 0:
                index, a, bb = 1, 1, 1
                while index < item:
                    a, bb = bb, a + bb
                    index += 1
                return a
        elif isinstance(item, slice):
            print(item.start, item.step, item.stop)
            start, step, stop = item.start, item.step, item.stop
            if item.start is None:
                start = 0
            if item.step is None:
                step = 1
            if item.stop is None:
                stop = 0
            if start < 0 or step <= 0 or stop < 0:
                raise TypeError("start<0||step<=0||stop<0")
            elif start + step > stop:
                raise TypeError("start + step > stop")
            else:
                result = []
                index, a, bb = 1, 1, 1
                while index <= stop:
                    a, bb = bb, a + bb
                    if index >= start and (index - start) % step == 0:
                        result.append(a)
                    index += 1
                return result

        return "不支持"


print(Fib()[4:-1:1])

  • 1

Reply