Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6172511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:24:27+00:00 2026-05-23T23:24:27+00:00

I’m trying to understand how yield works and after I had read this text

  • 0

I’m trying to understand how yield works and after I had read this text I believed that it’s quite understandable.

However I still don’t understand what’s the connection between yield and __iter__ because I’ve just found out that this code works:

class Vect():
    def __init__(self, *args):
        self.__a = list(args)
    def print_(self):
        print self.__a
    def __iter__(self):
        yield self.__a

asd = Vect(1,2,3,4,5)
for foo in asd:
    print foo

I thought that when I have a generator (a function which returns a single argument at the time but returns as many arguments as it can until it hits the end) yield works like: “OK, let’s return this argument, but maybe we can still return another”. However in my example I don’t have any generator, yield “returns” a list and in some way gets access to list’s iterator. I have absolutely no idea what’s going on.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:24:29+00:00Added an answer on May 23, 2026 at 11:24 pm

    yield returns whatever object is passed to it, even if that object is a sequence or a generator or other iterator. Thus:

    >>> def g():
    ...     yield [1,2,3]
    ...     yield 1
    ...     yield 2
    ...     yield 3
    ... 
    >>> gen = g()
    >>> gen.next()
    [1, 2, 3]
    >>> gen.next()
    1
    >>> gen.next()
    2
    >>> gen.next()
    3
    >>> gen.next()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    StopIteration
    >>>
    

    __iter__ is called on an object when an iterator over the object’s contents is required (as in a when it is part of a for x in obj construct). You can use yield to create a generator (since generators are iterators), but in the present example you don’t need to. The following will work as well:

    def __iter__(self):
       return iter(self.__a)
    

    If you want to use yield, and you want the iterator for Vect objects to move through the contents of the vector, you have to yield each value separately:

    def __iter__(self):
        for i in self.__a:
            yield i
    

    The yield means that __iter__ will return a generator, and calling next() on the generator object will resume the function at the point where it last left off, as it iterates through __a.

    =======

    In response to the additional question about how Python tracks where in the execution of the generator it is, I believe it uses the f_lasti (== “last instruction”) of the gi_frame attribute of the generator (Generators, unlike ordinary functions, carry a frame of execution around with them). Here’s a bit of tooling around that shows how the values change:

    >>> import dis
    >>> def g():
    ...     yield 1
    ...     for i in range(10):
    ...             yield i*2
    ... 
    >>> gen = g() 
    >>> dis.dis(gen.gi_code)
      2           0 LOAD_CONST               1 (1)
                  3 YIELD_VALUE         
                  4 POP_TOP             
    
      3           5 SETUP_LOOP              29 (to 37)
                  8 LOAD_GLOBAL              0 (range)
                  11 LOAD_CONST               2 (10)
                  14 CALL_FUNCTION            1
                  17 GET_ITER            
             >>   18 FOR_ITER                15 (to 36)
                  21 STORE_FAST               0 (i)
    
       4          24 LOAD_FAST                0 (i)
                  27 LOAD_CONST               3 (2)
                  30 BINARY_MULTIPLY     
                  31 YIELD_VALUE         
                  32 POP_TOP             
                  33 JUMP_ABSOLUTE           18
             >>   36 POP_BLOCK           
             >>   37 LOAD_CONST               0 (None)
                  40 RETURN_VALUE        
    >>> gen.gi_frame.f_lasti ## -1 because we haven't started yet
    -1 
    >>> gen.next()  
    1
    >>> gen.gi_frame.f_lasti
    3
    >>> gen.gi_frame.f_locals
    {}
    >>> gen.next() 
    0
    >>> gen.gi_frame.f_lasti , gen.gi_frame.f_locals
    (31, {'i': 0})
    >>> gen.next()
    2
    >>> gen.gi_frame.f_lasti , gen.gi_frame.f_locals
    (31, {'i': 1})
    >>> 
    

    Note how the f_lasti value corresponds to the numbered line in the disassembled code that that the last yield was on: it restarts from that point when the generator is reentered.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.