Conditional Assignment in If Statement

In the spirit of borrowing from other languages, there’s a particular bit of functionality from Swift that I’ve really wanted to have in Python. To preface, Swift uses var and let (static) when variables are created. It also supports optionals which allows a variable to be either some value or nil (Swift’s version of None). This enables the following syntax: if let foo = get_foo() { bar(foo) } In words: if the value returned by get_foo() is not nil, assign it to foo and enter the if block. The variable foo is static and only available within the scope of the if block. The closest thing we have in Python is: foo = get_foo() if foo is not None: bar(foo) However, foo is still available outside the scope of the if block presumably never to be referenced again. We could add “del foo” to remove it from our outer scope, but this is extra code. 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. Michael duPont

On Tue, Oct 18, 2016 at 9:11 AM, Michael duPont <michael@mdupont.com> wrote:
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

It was not my intention to declare those to be similar, just as a furthering train of thought. I agree that using "as" is a much more Pythonic syntax. I'm sure there was (and will be) some discussion as to whether it should operate like "if foo:" or "if foo is not None:". I'll look a bit further into the archives than I did to find previous discussions. For now, I'm a fan of: if get_foo() as foo: bar(foo) to replace the "if foo:" version: foo = get_foo() if foo: bar(foo) del foo On Mon, Oct 17, 2016 at 6:18 PM Chris Angelico <rosuav@gmail.com> wrote:

On 18.10.2016 00:11, Michael duPont wrote:
Before really understanding what you need here I have some questions: 1) What does real-world code look like here exactly? 2) Why do you need foo to be deleted after the if? 3) Do you need this in interactive sessions, short-lived code or maintained code? Cheers, Sven

On Tue, Oct 18, 2016 at 9:11 AM, Michael duPont <michael@mdupont.com> wrote:
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

It was not my intention to declare those to be similar, just as a furthering train of thought. I agree that using "as" is a much more Pythonic syntax. I'm sure there was (and will be) some discussion as to whether it should operate like "if foo:" or "if foo is not None:". I'll look a bit further into the archives than I did to find previous discussions. For now, I'm a fan of: if get_foo() as foo: bar(foo) to replace the "if foo:" version: foo = get_foo() if foo: bar(foo) del foo On Mon, Oct 17, 2016 at 6:18 PM Chris Angelico <rosuav@gmail.com> wrote:

On 18.10.2016 00:11, Michael duPont wrote:
Before really understanding what you need here I have some questions: 1) What does real-world code look like here exactly? 2) Why do you need foo to be deleted after the if? 3) Do you need this in interactive sessions, short-lived code or maintained code? Cheers, Sven
participants (3)
-
Chris Angelico
-
Michael duPont
-
Sven R. Kunze