<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 8 May 2018 at 04:19, Brett Cannon <span dir="ltr"><<a href="mailto:brett@python.org" target="_blank">brett@python.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div>My brain wants to drop the variable name in front of 'given':<br><br></div><div>if given m = pattern.search(data):<br><br></div><div>while given m = pattern.search(remaining_data)<wbr>:<br><br></div><div>Maybe it's because the examples use such a short variable name?<br></div></div></div></blockquote><div><br></div><div>Does that change if the condition isn't just "bool(name)"? For example:<br><br></div><div> if y > 0 given y = f(x):<br> ...<br><br></div><div>That's the situation where I strongly prefer the postfix operator spelling, since it's pretty clear how I should pronounce it (i.e. "if y is greater than zero, given y is set to f-of-x, then ..."). By contrast, while a variety of plausible suggestions have been made, I still don't really know how to pronounce "if (y := f(x)) > 0:)" in a way that's going to be clear to an English-speaking listener (aside from pronouncing it the same way as I'd pronounce the version using "given", but that then raises the question of "Why isn't it written the way it is pronounced?").<br><br></div><div>I do agree with Tim that the name repetition would strongly encourage the use of short names rather than long ones (since you're always typing them at least twice), such that we'd probably see code like:<br><br> while not probable_prime(n) given (n =<br>
highbit | randrange(1, highbit, 2)):<br>
pass<br><br></div><div>Rather than the more explicit:<br></div><div><br> while not probable_prime(candidate) given (candidate =<br>
highbit | randrange(1, highbit, 2)):<br>
pass<br><br></div><div>However, I'd still consider both of those easier to follow than:<br><br> while not probable_prime(candidate := highbit | randrange(1, highbit, 2)):<br>
pass<br><br></div><div>since it's really unclear to me that "candidate" in the latter form is a positional argument being bound to a name in the local environment, and *not* a keyword argument being passed to "probable_prime".<br></div><div></div><div><br></div><div>I've also been pondering what the given variant might look like as a generally available postfix operator, rather than being restricted to if/elif/while clauses, and I think that would have interesting implications for the flexibility of its usage in comprehensions, since there would now be *three* places where "given" could appear (as is already the case for the inline binding operator spelling):<br><br></div><div>- in the result expression<br></div><div>- in the iterable expression<br></div><div>- in the filter expression<br><br></div><div>That is:<br><br></div><div> [(x, y, x - y) given y = f(x) for x in data]<br></div><div><div> [(x, data) for x in data given data = get_data()]<br></div><div><div> [(x, y, x/y) for x in data if y given y = f(x)]<br><br></div><div>Rather than:<br><br><div> [(x, y := f(x), x - y) for x in data]<br></div><div><div> [(x, data) for x in data := get_data()]<br></div><div><div> [(x, y, x/y) for x in data if y := f(x)]<br></div></div></div><br></div><div>Opening it up that way would allow for some odd usages that might need to be discouraged in PEP 8 (like explicitly preferring "probable_prime(n) given n = highbit | randrange(1, highbit, 2)" to "probable_prime(n given n = highbit | randrange(1, highbit, 2))"), but it would probably still be simpler overall than attempting to restrict the construct purely to if/elif/while.<br><br></div><div>Even as a generally available postfix keyword, "given" should still be amenable to the treatment where it could be allowed as a variable name in a non-operator context (since we don't allow two adjacent expressions to imply a function call, it's only prefix keywords that have to be disallowed as names to avoid ambiguity in the parser).<br></div><div><br></div></div></div><div>Cheers,<br></div><div>Nick.<br clear="all"></div></div><br>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">Nick Coghlan | <a href="mailto:ncoghlan@gmail.com" target="_blank">ncoghlan@gmail.com</a> | Brisbane, Australia</div>
</div></div>