python如何制作一个任意列表_python如何将任意对象转换为列表?

文件

says:

The constructor builds a list whose items are the same and in the same

order as iterable‘s items. iterable may be either a sequence, a

container that supports iteration, or an iterator object. If iterable

is already a list, a copy is made and returned, similar to

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)

输出

__iter__ was called

__len__ was called

[1, 2, 3]

首先调用.__ iter__,好吧.但为什么A .__ len__被称为?然后为什么A .__ getitem__没被调用?

然后我把__iter__变成了发电机

而这改变了魔法方法调用的顺序!

class B(object):

def __iter__(self):

print '__iter__ was called'

yield 1

yield 2

yield 3

def __len__(self):

print '__len__ was called'

return 3

def __getitem__(self, index):

print '__getitem(%i)__ was called' % index

return index+1

b = B()

list(b)

输出

__len__ was called

__iter__ was called

[1, 2, 3]

为什么现在首先调用B .__ len__?但为什么然后B .__ getitem__没有被调用,转换是用B .__ iter__完成的?

最让我困惑的是为什么在A和B的情况下__len__和__iter__的调用顺序不同?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值