Proposed new syntax

Steve D'Aprano steve+python at pearwood.info
Sun Aug 20 22:00:38 EDT 2017


On Fri, 18 Aug 2017 04:55 pm, Marko Rauhamaa wrote:

> Is a Python implementation
> allowed to parallelize or otherwise reorder the evaluation loop?

No.

I initially was going to just say "Read the PEP, read the What's New from 2.0,
read the docs, notice the deliberate use of the same terminology as for-loops
and the similarity to generator expression syntax", but in fact there's a
simple demonstration for why list comps are not free to reorder the evaluation.


from io import StringIO  # simulate reading from a file
myfile = StringIO('Is this the room for an argument?')
values = [myfile.read(1) for i in range(33)]
print(''.join(values))


For all the talk of how people read list comps declaratively without caring
about the order the values are computed, if the above code snippet printed:

'nmoe sIsta  ret hhna fto?mri rugo'

instead of the expected 'Is this the room for an argument?', I believe that we
would all agree that behaviour a bug.

Perhaps not so much *here*, where we've paid for the full hour[1], but if it
happened to us in real-life code, certainly.




[1] "I'm sorry, I'm not allowed to argue unless you've paid."


-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list