求DEBUG ,__len__这节的问题
class Fib(object): def __init__(self, num): self.num = num if num == 1: self.L = [1] elif num == 2: self.L = [1, 1] else: self.L = [1, 1] for n in range(2, num): self.L.append(L[-2] + L[-1]) def __str__(self): return self.L def __len__(self): return self.num f = Fib(10) print f print len(f)
一直报错,求问这一段写的哪里有问题。错误如下
Traceback (most recent call last): File "C:/Users/tstcit/Desktop/Hello_2.7.py", line 20, in <module> f = Fib(10) File "C:/Users/tstcit/Desktop/Hello_2.7.py", line 12, in __init__ self.L.append(L[-2] + L[-1]) NameError: global name 'L' is not defined