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 <ros...@gmail.com> wrote: > On Tue, Oct 18, 2016 at 9:11 AM, Michael duPont <mich...@mdupont.com> > wrote: > > 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 > _______________________________________________ > Python-ideas mailing list > Python-ideas@python.org > https://mail.python.org/mailman/listinfo/python-ideas > Code of Conduct: http://python.org/psf/codeofconduct/ >
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/