[Python-ideas] the optional "as" statement inside "if" statements

fiatjaf at yahoo.com.br fiatjaf at yahoo.com.br
Sat Jun 30 17:46:08 CEST 2012


thank you two for the responses.

I'm a newbie here and I didn't find the archives (yes, I'm stupid, and I
didn't search well).
I have no hope of being persuasive, I only thought that if I introduced the
idea other people would like it instantenously, but if the idea is good,
obviously someone had already thought of it, so it makes me happy. I'll
look for the archives and keep watching the development and see what I get
from this.

On Sat, Jun 30, 2012 at 12:06 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:

> On Sat, Jun 30, 2012 at 11:59 PM,  <fiatjaf at yahoo.com.br> wrote:
> > the idea is to make an variable assignment at the same time that the
> > existence of that variable -- which is being returned by a function -- is
> > made.
> >
> > suppose we are returning a variable from the method 'get' from the
> 'request'
> > object and them making some stuff with it, but that stuff we will only
> do if
> > it exists, if not, we'll just pass, instead of writing:
> >
> > variable = self.request.get('variable')
> > if variable:
> >    print variable
> >
> > we could write
> >
> > if self.request.get('variable') as variable:
> >    print variable
> >
> > seems stupid (or not?), but with lots of variables to process, this
> > pre-assignment could be very unpleasant -- especially if, as the in the
> > example case, very little use will be made of the tested variable.
> >
> > also, the "as" expression already exists and is very pythonic.
>
> This proposal has been considered and rejected many times. It's not
> general enough - it *only* works for those cases where the value to be
> retained *and* the interesting condition are the same.
>
> Consider the simple case of a value that may be either None (not
> interesting) or a number (interesting). Since the interesting values
> include "0", which evaluates as False along with None, this limited
> form of embedded assignment syntax would not help.
>
> Embedded assignment in C isn't that limited., but nobody has yet
> volunteered to take the radical step of proposing "(X as Y)" as a
> general embedded assignment syntax. I suggest anyone consider such an
> idea do a *lot* of research in the python-ideas archives first, though
> (as the idea has seen plenty of discussion). It is not as obviously
> flawed as the if-and-while statement only variant, but it would still
> involve being rather persuasive to make such a significant change to
> the language.
>
> You're also unlikely to get much in the way of core developer feedback
> until after the 3.3 release in August.
>
> Cheers,
> Nick.
>
>
>
> --
> Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120630/53915f04/attachment.html>


More information about the Python-ideas mailing list