Possible Addition to Python Language: Marked Sub-condition
Terry Reedy
tjreedy at udel.edu
Sun Mar 8 13:03:09 EDT 2020
On 3/8/2020 12:07 PM, MRAB wrote:
> In Python 3.8+ there's the "walrus operator" which lets you assign
> within an expression:
>
> if (first := (color == BLUE and count == 20)) or (second := (color ==
> RED and count % 5 == 0)):
> rotate_the_wheel()
> if first:
> set_signal()
> if second:
> unset_signal()
> proc_post_rotate()
>
> However, this has the problem that if the first subexpression is true,
> the second will not be evaluated, so 'second' would not be set.
One can use '|' instead of 'or' to force evaluation of the second.
>>> if (first := True) | (second := False):
second
False
--
Terry Jan Reedy
More information about the Python-list
mailing list