Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #82235
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: Python Iterables struggling using map() built-in |
| Date | 2014-12-09 15:06 -0500 |
| References | (3 earlier) <mailman.16685.1417976998.18130.python-list@python.org> <5484e3d8$0$13013$c3e8da3$5496439d@news.astraweb.com> <roy-3F4751.19122007122014@news.panix.com> <54866372$0$12991$c3e8da3$5496439d@news.astraweb.com> <m65vrc$j0t$1@ger.gmane.org> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.16776.1418155635.18130.python-list@python.org> (permalink) |
On 12/9/2014 12:03 AM, Terry Reedy wrote:
>> Roy Smith wrote:
>>
>>> Chris Angelico wrote:
>
>>>> def myzip(*args):
>>>> iters = map(iter, args)
>>>> while iters:
>>>> res = [next(i) for i in iters]
>>>> yield tuple(res)
>>>
>>> Ugh. When I see "while foo", my brain says, "OK, you're about to see a
>>> loop which is controlled by the value of foo being changed inside the
>>> loop".
What is nasty to me is that to understand the loop, one must do a whole
program analysis to determine both that 'iters' is not rebound and that
the list it is bound to is not mutated. To do the later, one must not
only read the loop body, but also preceding code to make sure the list
is not aliased.
> iters is empty if and only if args is empty.
> If args is empty, iters should not be created.
>
> if args:
> iters = ...
> while True
> ... (return on exception)
>
> makes the logic clear.
Once the logic is clear and 'localized', even a simple compiler like
CPython's can see that this is a loop-forever construct and that the
loop test is unnecessary. So it can be removed.
>>> dis("while a: b+=1")
1 0 SETUP_LOOP 20 (to 23)
>> 3 LOAD_NAME 0 (a)
6 POP_JUMP_IF_FALSE 22
9 LOAD_NAME 1 (b)
12 LOAD_CONST 0 (1)
15 INPLACE_ADD
16 STORE_NAME 1 (b)
19 JUMP_ABSOLUTE 3
>> 22 POP_BLOCK
>> 23 LOAD_CONST 1 (None)
26 RETURN_VALUE
>>> dis("while True: b+=1")
1 0 SETUP_LOOP 13 (to 16)
>> 3 LOAD_NAME 0 (b)
6 LOAD_CONST 0 (1)
9 INPLACE_ADD
10 STORE_NAME 0 (b)
13 JUMP_ABSOLUTE 3
>> 16 LOAD_CONST 1 (None)
19 RETURN_VALUE
'while 1' and 'while "exception not raised"' are similarly condensed.
This leaves only the initial test of the argument.
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: Python Iterables struggling using map() built-in Chris Angelico <rosuav@gmail.com> - 2014-12-08 05:29 +1100
Re: Python Iterables struggling using map() built-in Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-12-08 10:33 +1100
Re: Python Iterables struggling using map() built-in Chris Angelico <rosuav@gmail.com> - 2014-12-08 10:50 +1100
Re: Python Iterables struggling using map() built-in Roy Smith <roy@panix.com> - 2014-12-07 19:27 -0500
Re: Python Iterables struggling using map() built-in Chris Angelico <rosuav@gmail.com> - 2014-12-08 11:35 +1100
Re: Python Iterables struggling using map() built-in Steven D'Aprano <steve@pearwood.info> - 2014-12-08 09:40 +0000
Re: Python Iterables struggling using map() built-in Chris Angelico <rosuav@gmail.com> - 2014-12-08 20:50 +1100
Re: Python Iterables struggling using map() built-in Roy Smith <roy@panix.com> - 2014-12-08 19:02 -0500
Re: Python Iterables struggling using map() built-in Roy Smith <roy@panix.com> - 2014-12-07 19:12 -0500
Re: Python Iterables struggling using map() built-in Chris Angelico <rosuav@gmail.com> - 2014-12-08 11:34 +1100
Re: Python Iterables struggling using map() built-in Roy Smith <roy@panix.com> - 2014-12-07 19:45 -0500
Re: Python Iterables struggling using map() built-in Chris Angelico <rosuav@gmail.com> - 2014-12-08 12:00 +1100
Re: Python Iterables struggling using map() built-in MRAB <python@mrabarnett.plus.com> - 2014-12-08 03:09 +0000
Re: Python Iterables struggling using map() built-in Terry Reedy <tjreedy@udel.edu> - 2014-12-07 19:38 -0500
Re: Python Iterables struggling using map() built-in Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-12-08 18:06 +1300
Re: Python Iterables struggling using map() built-in Ned Batchelder <ned@nedbatchelder.com> - 2014-12-07 20:06 -0500
Re: Python Iterables struggling using map() built-in Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-12-09 13:50 +1100
Re: Python Iterables struggling using map() built-in Terry Reedy <tjreedy@udel.edu> - 2014-12-09 00:03 -0500
Re: Python Iterables struggling using map() built-in Steven D'Aprano <steve@pearwood.info> - 2014-12-09 06:40 +0000
Re: Python Iterables struggling using map() built-in Terry Reedy <tjreedy@udel.edu> - 2014-12-09 15:06 -0500
Re: Python Iterables struggling using map() built-in Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-12-10 11:10 +1100
Re: Python Iterables struggling using map() built-in Chris Angelico <rosuav@gmail.com> - 2014-12-10 11:20 +1100
Re: Python Iterables struggling using map() built-in Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-12-10 00:24 +0000
Re: Python Iterables struggling using map() built-in Roy Smith <roy@panix.com> - 2014-12-09 21:44 -0500
Re: Python Iterables struggling using map() built-in Steven D'Aprano <steve@pearwood.info> - 2014-12-10 09:24 +0000
Re: Python Iterables struggling using map() built-in Chris Angelico <rosuav@gmail.com> - 2014-12-10 20:32 +1100
Re: Python Iterables struggling using map() built-in Marko Rauhamaa <marko@pacujo.net> - 2014-12-10 12:58 +0200
Re: Python Iterables struggling using map() built-in Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-10 09:46 -0700
Re: Python Iterables struggling using map() built-in Steven D'Aprano <steve@pearwood.info> - 2014-12-11 04:01 +0000
Re: Python Iterables struggling using map() built-in Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-11 15:00 -0700
Re: Python Iterables struggling using map() built-in Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-12-12 10:28 +1100
Re: Python Iterables struggling using map() built-in Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-11 16:57 -0700
Re: Python Iterables struggling using map() built-in Roy Smith <roy@panix.com> - 2014-12-11 20:55 -0500
Re: Python Iterables struggling using map() built-in Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-12 00:36 -0700
Re: Python Iterables struggling using map() built-in Chris Angelico <rosuav@gmail.com> - 2014-12-12 11:44 +1100
Re: Python Iterables struggling using map() built-in Terry Reedy <tjreedy@udel.edu> - 2014-12-10 12:48 -0500
Re: Python Iterables struggling using map() built-in Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-10 13:32 -0700
Re: Python Iterables struggling using map() built-in Terry Reedy <tjreedy@udel.edu> - 2014-12-10 23:17 -0500
Re: Python Iterables struggling using map() built-in Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-12-08 18:00 +1300
csiph-web