Discuss / Python / 请问__call__能返回到函数吗??

请问__call__能返回到函数吗??

Topic source

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

请问是call写的return self.iter不对吗

廖雪峰

#2 Created at ... [Delete] [Delete and Lock User]

__call__能返回iter,但是for语句不会调用__call__


  • 1

Reply