[Python-ideas] Where-statement (Proposal for function expressions)

Chris Perkins chrisperkins99 at gmail.com
Thu Jul 16 17:21:10 CEST 2009


On Thu, Jul 16, 2009 at 11:06 AM, Paul Moore<p.f.moore at gmail.com> wrote:
>
> So where constructs are only allowed for code of the form [LHS OP] EXPR?
>
> What does that correspond to in the grammar? (If it doesn't correspond
> to something at the moment, you'll need to extend the grammar to
> define a "thing that can have a where clause" production...)

Without having thought too hard about it, I would allow a where block
after a "simple_stmt" - same thing I did with my "do block" proposal.
That means you could have a where after any of these:

small_stmt: (expr_stmt | print_stmt  | del_stmt | pass_stmt | flow_stmt |
             import_stmt | global_stmt | exec_stmt | assert_stmt)

So by EXPR above, I guess I was thinking of any "small_stmt"
production. Of course, it makes no sense for some of them, like
pass_stmt, global_stmt, etc, but I don't see any harm in allowing the
block to be there - I don't think I could have any effect though.

> You've also disallowed
>
> x[i] = 12 where:
>    i = some_complicated_expression()
>
> Was that deliberate? If so, could you explain why now, so that it's on
> record for the legions of people who will ask after it's implemented?
> :-)

Well, my original answer was "no, I just didnt' think of that'.  But
now that you mention it, I think in the case of an "augassign"
production, it makes sense to have the left hand side evaluated in the
current scope (not in the 'where block" scope).  So in your examle,
you would either get a NameError for i, the code would do somthing
that you didn't expect.


Chris Perkins



More information about the Python-ideas mailing list