[Python-ideas] If branch merging
rosuav at gmail.com
Sun Jun 7 14:20:54 CEST 2015
On Sun, Jun 7, 2015 at 8:20 PM, s.krah <stefan at bytereef.org> wrote:
> Steven D'Aprano <steve at pearwood.info> wrote:
> On Sat, Jun 06, 2015 at 11:40:55PM -0400, random832 at fastmail.us wrote:
>>> Well you could always go with if aisb = a == b.
>> No, that is a terrible design and a source of many bugs in languages
>> that allow it.
>> if a = expr: ...
>> Oops, I meant to compare a == expr, instead I assigned the result of the
>> expression to a.
> In C I've mistyped this perhaps twice, in which case you get a compiler
> It's a complete non-issue (and the construct *is* very handy).
That's as may be, but Steven's still correct that the Pythonic way to
do it would be with "as". In C, assignment is an expression ("=" is an
operator that mutates its LHS and yields a value), but in Python, it
simply isn't, and making it possible to do assignment in an 'if'
condition would be a big change.
with expr as name:
except expr as name:
if expr as name:
Three parallel ways to do something and capture it. It makes
reasonable sense, if someone can come up with a really compelling
use-case. Personally, I'd be more inclined to seek the same thing for
a while loop:
while get_next_value() as value:
# equivalent to
value = get_next_value()
if not value: break
as that's a somewhat more common idiom; but neither is hugely common.
More information about the Python-ideas