Python----iter\next

Source: Internet
Author: User

1, Description: __getitem__\setitem can iterate, it has not been recommended; __iter__\next is recommended.

2, Python will first check __iter__\next and then go to check __getitem__\__setitem__, that is __iter__ priority.

Example:

#!coding:utf-8#!python version 2.7classmysqence (object):def __init__(self,start,stop): Self.start=Start Self.stop=Stop Self.current=Startdef __iter__(self):return SelfdefNext (self):" "if it's a python3.0 environment, it's not using next anymore, it's changing the name to __next__." " Current=self.currentifCurrent <=self.stop:self.current=self.current+1return CurrentElse:            Raisestopiteration forXinchMysqence (1,5):    PrintX

Python----iter\next

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.