[Python-ideas] Inline assignments using "given" clauses
Chris Angelico
rosuav at gmail.com
Fri May 11 17:44:24 EDT 2018
On Sat, May 12, 2018 at 7:12 AM, Angus Hollands <goosey15 at gmail.com> wrote:
> e.g
>
> while value.in_use given value = get_next_pool_item():
> print(value.refcount())
>
> Instead of
>
> while (value:=get_next_pool_item()) and value.in_use:
> print(value.refcount())
>
> In addition, the two cases are not identical - if the API is such that
> get_next_pool_item should not return None values, this wouldn't be caught in
> the ":=" approach, unless you add more conditions. Yes, you could argue that
> I've cherry picked an example here. Actually, I haven't; I observed this
> after writing the example.
>
> What am I getting at here? In effect, the "given" keyword provides a
> superset of use cases to that of ":=". Dare I say it, but explicit is better
> than implicit.
I'm not sure what you're getting at here. To make your two versions
equivalent, you'd need to write them like this:
while value and value.in_use given value = get_next_pool_item():
print(value.refcount())
while (value:=get_next_pool_item()) and value.in_use:
print(value.refcount())
or like this:
while value.in_use given value = get_next_pool_item():
print(value.refcount())
while (value:=get_next_pool_item()).in_use:
print(value.refcount())
There, now they're identical. There's no superset or subset of use
cases. And I have no idea how that connects with the oft-misused
"explicit is better than implicit".
(I'm still fairly sure that "explicit" and "strongly typed" are both
synonyms for "stuff I like", with their antonyms "implicit" and
"weakly typed" both being synonyms for "stuff I don't like". Years of
discussion have not disproven this theory yet.)
ChrisA
More information about the Python-ideas
mailing list