Discuss / Python / 想请教一下关于__getitem__这部分的一些问题

想请教一下关于__getitem__这部分的一些问题

Topic source
class Fib(object):    def __getitem__(self, n):        if isinstance(n, int):            a, b = 1, 1            for x in range(n):                a, b = b, a+b            return a        if isinstance(n, slice):            start = n.start            stop = n.stop            if start is None:                start = 0            a, b = 1, 1            L = []            for x in range(stop):                if x >= start:                    L.append(a)                a, b = b, a+b                f = Fib()print(f[:6])

Fib()的代码是我直接照着课程里的代码敲的,为什么打印f[:6]的时候会是 None呢?

还有就是输入时slice的时候,没有定义start和stop吧,为什么能够把list的参数的第一个值和第二个值传到start和stop呢?

迷惑,请教各位大佬


  • 1

Reply