Negative subscripts

dn PythonList at DancesWithMice.info
Mon Nov 29 14:44:03 EST 2021



On 27/11/2021 21.23, Chris Angelico wrote:
> On Sat, Nov 27, 2021 at 7:21 PM dn via Python-list
> <python-list at python.org> wrote:
>> The expression list is evaluated once; it should yield an iterable
>> object. An iterator is created for the result of the expression_list.
>> The suite is then executed once for each item provided by the iterator,
>> in the order returned by the iterator. Each item in turn is assigned to
>> the target list using the standard rules for assignments (see Assignment
>> statements), and then the suite is executed. When the items are
>> exhausted (which is immediately when the sequence is empty or an
>> iterator raises a StopIteration exception), the suite in the else
>> clause, if present, is executed, and the loop terminates.
>> »
>> https://docs.python.org/3/reference/compound_stmts.html#the-for-statement
>>
>>
>> That said, I'm wondering if all is strictly true when the
>> expression_list is a generator, which by definition features
>> lazy-execution rather than up-front list production. So, things may
>> depend upon the full-nature of the application.
> 
> Yes, it is. Evaluating a generator expression gives you a generator
> object, which is an iterable.


You (and the text) are correct - the expression list is "evaluated once"
and produces a generator object. For a particular understanding of
"evaluate".

However, as described, all that has been "evaluated" is a
generator-object. Unlike (say) a list's iterator, a generator only
eventually produces a 'list' - and each time the generator is
called-upon to yield the next value, that value has to be "evaluated",
ie there's some further evaluation loop-by-loop.

Further, that the values-returned can be amended during the life of the
generator (in ways anticipated and unanticipated by coder and
interpreter alike). Thus, it seems that the "list" doesn't actually
exist, as in, hasn't been "evaluated" as data-values, when the loop is
enacted.

What has been 'evaluated' are the terms by which the looping will
proceed, and terminate.


-- 
Regards,
=dn


More information about the Python-list mailing list