[Python-Dev] assignment expressions: an alternative proposal
Steven D'Aprano
steve at pearwood.info
Tue Apr 24 12:38:03 EDT 2018
On Tue, Apr 24, 2018 at 11:35:20AM -0400, Yury Selivanov wrote:
> Yes, it would force users to come up with better names *iff* they want
> to use this new sugar:
>
> if (first_target = get_first_candidate()) ...
> elif (second_target = get_second_candidate()) ...
They're not better names. Adding "first_" and "second_" prefixes are
just meaningless waffle added to the name "target" to satisfy the
compiler so it doesn't complain about reusing the name.
And it is a clear inconsistency with = as a statement and = as an
expression:
# allowed
target = get_first_candidate()
if target:
...
else:
target = get_second_candidate()
if target: ...
# refactor, and we get a syntax error
if (target = get_first_candidate()):
...
elif (target = get_second_candidate()):
...
And I cannot even begin to guess whether this will be allowed or not:
if (target = get_first_candidate()):
...
while (target = get_second_candidate()):
...
--
Steve
More information about the Python-Dev
mailing list