On Tue, Oct 18, 2016 at 9:11 AM, Michael duPont
What does everyone think about:
if foo = get_foo(): bar(foo)
as a means to replace:
foo = get_foo() if not foo: bar(foo) del foo
Might there be some better syntax or a different keyword? I constantly run into this sort of use case.
I'm pretty sure that syntax is never going to fly, for a variety of reasons (to see most of them, just read up a C style guide). But this syntax has been proposed now and then, analogously with the 'with' statement: if get_foo() as foo: bar(foo) Be careful of your definitions, though. You've said these as equivalent: if foo = get_foo(): bar(foo) foo = get_foo() if foo is not None: bar(foo) foo = get_foo() if not foo: bar(foo) del foo There are three quite different conditions here. Your last two are roughly opposites of each other; but also, most people would expect "if foo = get_foo()" to be the same condition as "if get_foo()", which is not the same as "if get_foo() is not None". The semantics most likely to be accepted would be for "if get_foo() as foo:" to use the standard boolification rules of Python (and then make 'foo' available in both 'if' and 'else' blocks). Would you support that? If so, check out some of the previous threads on the subject - this is far from the first time it's been discussed, and most likely won't be the last. ChrisA