What does the in keyword mean in a Python for loop?

Source: Internet
Author: User
The most commonly used cases, I understand, such as

 for I inch Range (1,5):    Print I

Reply content:

Keywords: iterators

In simple terms, the for in statement is a syntactic sugar, specifically:
    • Calling an object's __iter__ method, the method returns an iterator that is the object that implements the __next__ method, and if an object itself implements the __next__ (a direct "next" method in Python 2, without an underscore), it can return itself directly.
    • The __next__ of the call iterator returns the "next" element in the iterator, for example, the first call returns 0, and the second returns 1, and so on.
    • There is no element at the end, and the iterator throws an exception to indicate that it has no elements. The For statement catches the exception and stops.
I suggest you write a Fibonacci iterator on your own.
Mac How to make normal size AH Qaq

In addition, there is a ecstasy called the generator, demonstrating how gracefully Fibonacci:
(Since then the code for brevity I have written in Python, with Python 2 can run but poor performance.) )
deffib(n):   a=0   b=1   for_inrange(n):       a,b=b,a+b       yielda
Thank you for your invitation. Just see the problem think the landlord to ask in what meaning.
This for is actually an iteration, using an iterator (Iterator).
# 以下代码在Python 2中运行for row in f: print row# 完全等价于itr = f.__iter__() # 获得新的迭代器while True: try: row = itr.next() except StopIteration: break print row
Iterators.

Will
Instructions for turning the dis into a virtual machine
Discovery is Get_iter, For_iter and the like. Discovery is Get_iter, For_iter and the like this is the range (1:5) and F as a container to see. Since there is a line in the file F, the row is naturally a string. As a personal understanding:
inchKeyword implements a set of traversal protocols in Python.
    • Protocol A: __iter__ + next
Loop, the program uses __iter__ (equivalent to ITER (instance)) to obtain an object with the next method, and then, through its returned object, calls its next method continuously until Stopiteration error thrown.

class A:    def __iter__( Self):         Self.Limit = 4         Self. Times = 0         Self.Init = 1        return  Self    def Next( Self):        if  Self. Times >=  Self.Limit:            Raise stopiteration()        Else:            x =  Self.Init             Self. Times += 1             Self.Init += 1            return xPrint ' a>>>>>> ' for x inch A():    Print x
  • 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.