<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">2017-11-29 22:33 GMT+03:00 Steve Barnes <span dir="ltr"><<a href="mailto:gadgetsteve@live.co.uk" target="_blank">gadgetsteve@live.co.uk</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="gmail-HOEnZb"><div class="gmail-h5">
<br>
</div></div>Just a thought but what about a syntax something along the lines of:<br>
<br>
a, b, *remainder = iterable<br>
<br>
Where remainder becomes the iterable with the first two values consumed<br>
by assigning to a & b. If the iterator has less than 2 values, (in the<br>
above case), remaining it should error, if it has exactly 2 then<br>
remainder would become an exhausted iterable. Of course the user could<br>
also use:<br>
<br>
a, b, *iterable = iterable<br>
<br>
Others may differ but this syntax has a lot of similarity to the f(a, b,<br>
*args) syntax, possibly enough that most users could understand it.<br></blockquote><div><br></div><div> </div></div><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">Before I started this thread, </span></span><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">not so long ago, </span></span> I have already asked a question about this semantics [<a href="https://mail.python.org/pipermail/python-ideas/2017-November/048027.html">1</a>]. <br></span></span></div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">But it appears to be</span></span><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"> very ambiguous in practice for the various rhs:</span></span></div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><br></span></span></div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">...<br></span></span></div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">x, *y, z = some_iter<br></span></span></div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">*x , y, z = some_iter<br></span></span></div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">x, y, *z = some_iter<br></span></span></div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><br></span></span></div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">And only for the last case it will mean something special.</span></span><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"></span></span><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"> In addition, it is a huge backward compatibility break.</span></span></div><div class="gmail_extra"><div><br></div><div><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">Probably, </span></span><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">some time ago</span></span> it was necessary to split this thread into two questions:</span></span></div><div><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">1. </span></span><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">Philosophical question regarding sequences and iterators.</span> <span class="gmail-">In particular, should </span></span></span></span><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">they</span></span></span></span> behave differently depending on the context, </span></span></span></span><br><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">or,
 in other words, whether to emphasize their different nature as 
fixed-size containers and those that are lazily produce values on 
demand.</span></span></span></span></span></span></div><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">2. </span></span></span></span></span></span><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">Additional syntax in the assignment statement for partial extraction of values from the iterable.</span></span><br><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"></span></span><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-short_text" lang="en"><span class="gmail-"></span></span></span></span></div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><br></span></span></div><div><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">2017-11-30 22:19 GMT+03:00 Paul Moore <span dir="ltr"><<a href="mailto:p.f.moore@gmail.com" target="_blank">p.f.moore@gmail.com</a>></span>:</span></span></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"></span></span><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"></span></span> <br></div></blockquote><div class="gmail_extra"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">Mostly corner cases, and I don't believe there have been any</span></span> <span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">non-artificial examples posted in this thread.</span></span> </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"> Certainly no-one has </span></span><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">offered a real-life code example that is made significantly worse by</span></span><br><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">
the current semantics, and/or which couldn't be easily worked around</span></span><br><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">
without needing a language change.</span></span></blockquote><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"></span></span><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"></span></span><br></div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-short_text" lang="en"><span class="gmail-">Yes, in fact, this is a good question,</span></span> is whether that is sufficiently useful to justify extending the syntax. But it is not about corner cases, it is rather usual situation.</div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">Nevertheless, this is the most difficult moment for Rationale. </span></span><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">By now, this feature does not give you new opportunities for solving problems.</span> <span class="gmail-">It's more about expressiveness and </span></span></span></span><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-short_text" lang="en"><span class="gmail-">convenience</span></span>. You can write:</span></span></span></span></div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><br></span></span></span></span></div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">x, y, ... = iterable</span></span></span></span></div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><br></span></span></span></span></div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">or,</span></span></span></span></div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><br></span></span></span></span></div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">it = iter(iterable)<br></span></span></span></span></div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">x, y = next(it), next(it)<br></span></span></span></span></div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><br></span></span></span></span></div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">or,</span></span></span></span></div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><br></span></span></span></span></div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">from itertools import isclice</span></span></span></span></div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">x, y = islice(iterable, 2)</span></span></span></span></div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><br></span></span></span></span></div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">or, <br></span></span></span></span></div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">x, y = iterable[:2]<br></span></span></span></span></div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><br></span></span></span></span></div><div class="gmail_extra"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">and others, also in some cases when you have infinite generator or iterator, you should use 2nd or 3rd. </span></span></span></span><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">In fact, this has already been said and probably I will not explain it better:<br></span></span></span></span></span></span></div><div class="gmail_extra"><div><br><div class="gmail_quote">2017-11-28 1:40 GMT+03:00 Greg Ewing <span dir="ltr"><<a href="mailto:greg.ewing@canterbury.ac.nz" target="_blank">greg.ewing@canterbury.ac.nz</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span class="gmail-">Guido van Rossum wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Is this problem really important enough that it requires dedicated syntax? Isn't the itertools-based solution good enough?<br>
</blockquote>
<br></span>
Well, it works, but it feels very clumsy. It's annoying to<br>
have to specify the number of items in two places.<br>
<br>
Also, it seems perverse to have to tell Python to do *more*<br>
stuff to mitigate the effects of stuff it does that you<br>
didn't want it to do in the first place.<br>
<br>
Like I said, I'm actually surprised that this doesn't already<br>
work. To me it feels more like filling in a piece of<br>
functionality that was overlooked, rather than adding a<br>
new feature. Filling in a pothole in the road rather than<br>
bulding a new piece of road.<br>
<br>
(Pushing the road analogy maybe a bit too far, the current<br>
itertools solution is like digging *more* potholes to make<br>
the road bumpy enough that you don't notice the first<br>
pothole.)<span class="gmail-"><br>
<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
(Or failing that, couldn't we add something to itertools to make it more readable rather than going straight to new syntax?)<br>
</blockquote>
<br></span>
I'm not sure how we would do that. Even if we could, it<br>
would still feel clumsy having to use anything from itertools<br>
at all.</blockquote></div></div><div><br></div><div> With kind regards, -gdg<br></div></div></div>