温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python中for循环与getitem的关系是什么

发布时间:2021-01-18 15:54:51 来源:亿速云 阅读:456 作者:Leah 栏目:开发技术

今天就跟大家聊聊有关Python中for循环与getitem的关系是什么,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

一个类里面如果由__iter__for循环就是找它取,没有的话就会找__getitem__

前面一笔看过没有留心具体的执行情况。

In [169]: class Foo:    ...:   def __getitem__(self, pos):    ...:     print(pos)    ...:     return range(10)[pos]    ...:
In [172]: for i in f:    ...:   ...    ...:       ...:                                              0 1 2 3 4 5 6 7 8 9 10

从代码可以看出,如果没有报错或者设置显式的条件,这个for循环会无线循环。

我现在设置一个显式的设置。

In [173]: class Foo:    ...:   def __getitem__(self, pos):    ...:     if pos >5:    ...:       raise StopIteration    ...:     print(pos)    ...:     return range(10)[pos]    ...:
In [177]: for i in f:    ...:   ...    ...:                                              0 1 2 3 4 5

将错误设置为IndexError也可以执行,但TypeError就不行了。

   ...:   def __getitem__(self, pos):    ...:     if pos >5:    ...:       raise IndexError    ...:     print(pos)    ...:     return range(10)[pos]    ...:                                                In [182]:                                                In [182]: f = Foo()                                           In [183]: for i in f:    ...:   ...    ...:                                              0 1 2 3 4 5

如果用list去运行这个参数会把返回的一个一个元素,装入列表当中:

In [184]: list(f)                                          0 1 2 3 4 5 Out[184]: [0, 1, 2, 3, 4, 5]

只有__getitem__的类的实例是属于可迭代对象,但用isinstances测试collections.Iterable是不能通过的,书后面介绍可以通过iter函数来测试,如果没报错就说明是可迭代对象,然后生成一个没有__next__属性的迭代器。

In [185]: from collections import Iterable                             In [186]: isinstance(f, Iterable)                                  Out[186]: False   In [187]: iter(f)                                          Out[187]: <iterator at 0x114f2be50>
dir(f)                                          Out[189]: ['__class__',  '__delattr__',  '__dict__',  '__dir__',  '__doc__',  '__eq__',  '__format__',  '__ge__',  '__getattribute__',  '__getitem__',  '__gt__',  '__hash__',  '__init__',  '__init_subclass__',  '__le__',  '__lt__',  '__module__',  '__ne__',  '__new__',  '__reduce__',  '__reduce_ex__',  '__repr__',  '__setattr__',  '__sizeof__',  '__str__',  '__subclasshook__',  '__weakref__']

看完上述内容,你们对Python中for循环与getitem的关系是什么有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI