New syntax for blocks

samwyse samwyse at gmail.com
Wed Nov 11 08:11:21 EST 2009


On Nov 10, 1:23 pm, r <rt8... at gmail.com> wrote:
> Forgive me if i don't properly explain the problem but i think the
> following syntax would be quite beneficial to replace some redundant
> "if's" in python code.
>
> if something_that_returns_value() as value:
>     #do something with value
>
> # Which can replace the following syntactical construct...
>
> value = something_that_returns_value()
> if value:
>     #do something with value

I don't like the proposed syntax.  I know, it's copied from the "with"
statement, but it makes the assignment look like an afterthought.
Plus, it's not good English when read aloud.

If you want something useful, wait two years for the moratorium to
expire and then figure out how to augment the "for" statement with an
"if" clause, as is currently done in comprehensions.  In other words,
instead of this:
  "for" target_list "in" expression_list ":" suite
let's have this:
  "for" target_list "in" expression_list [ "if" expression_nocond ]
":" suite

You don't save much typing, but you really do save one indention
level.  OTOH, I can see the use of an else-clause following the 'for'
as being even more confusing to anyone new to the language.

And there's also the fact that an expression_list consists of
conditional_expressions, which potentially use "if".  You could
probably get the parser to figure it out based on the presence of the
"else" clause, but it wouldn't be easy.



More information about the Python-list mailing list