构造函数构建一个列表,其项目与iterable的项目相同且顺序相同.iterable可以是序列,支持迭代的容器,也可以是迭代器对象.如果iterable已经是一个列表,则会生成并返回一个副本,类似于iterable [:] ...
但是如果我有一个a我的类的对象A,那就实现了__iter__,__len__并且__getitem__,使用哪个接口list(a)来迭代我的对象以及它背后的逻辑是什么?
class A(object):
def __iter__(self):
print '__iter__ was called'
return iter([1,2,3])
def __len__(self):
print '__len__ was called'
return 3
def __getitem__(self, index):
print '__getitem(%i)__ was called' % index
return index+1
a = A()
list(a)
Run Code Online (Sandbox Code Playgroud)
输出
__iter__ was called
__len__ was called
[1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
A.__iter__被称为第一,好的.但为什么那么A.__len__被称为?然后为什么A.__getitem__没有被召唤?
__iter__发电机而这改变了魔法方法调用的顺序!
class B(object):
def __iter__(self):
print '__iter__ was …Run Code Online (Sandbox Code Playgroud)