class Fib(object): def init(self): self.a,self.b=1,1 def iter(self): return self def next(self): self.a,self.b=self.b,self.a+self.b if self.a>1000: raise StopIteration return self.a def call(self): return self.iter
f=Fib() print(f()) print(callable(f))
f()输出的结果是
print(f()) <bound method Fib.__iter__ of <__main__.Fib object at 0x023BA310>> print(callable(f)) True
print(f())
<bound method Fib.__iter__ of <__main__.Fib object at 0x023BA310>> print(callable(f)) True
请问是call写的return self.iter不对吗
__call__能返回iter,但是for语句不会调用__call__
__call__
Sign in to make a reply
试试能不能注册
class Fib(object): def init(self): self.a,self.b=1,1 def iter(self): return self def next(self): self.a,self.b=self.b,self.a+self.b if self.a>1000: raise StopIteration return self.a def call(self): return self.iter
f=Fib() print(f()) print(callable(f))
f()输出的结果是
请问是call写的return self.iter不对吗