[Python-ideas] 'where' statement in Python?

Nick Coghlan ncoghlan at gmail.com
Wed Jul 21 13:37:57 CEST 2010


On Tue, Jul 20, 2010 at 11:52 AM, Jack Diederich <jackdied at gmail.com> wrote:
> I think the "trick" to making it readable is putting the assignment first.
>
> par_pos = decl.find('(')
> vtype = decl[par_pos+1:FindMatching(par_pos, decl)].strip()
>
> versus:
>
> vtype = decl[par_pos+1:FindMatching(par_pos, decl)].strip() where
> par_pos=decl.find('(')

Note that with a "given" clause, I would recommend writing something
along these lines:

vtype = decl[open_paren_pos+1:close_paren_pos] given:
    open_paren_pos = decl.find('(')
    close_paren_pos = FindMatching(open_paren_pos, decl)

The positions of the open and closing parentheses are only relevant in
the assignment statement and you can understand what the code does
based just on the names of the subexpressions without necessarily
worrying about how they are determined.

The question here is whether this offers *enough* benefit over just writing

open_paren_pos = decl.find('(')
close_paren_pos = FindMatching(open_paren_pos, decl)
vtype = decl[open_paren_pos+1:close_paren_pos]

to be worth the significant additional complexity it introduces.

Currently I'd say the scales are leaning heavily towards "not worth
the hassle", but I'd be interesting to see what people can make of the
PEP 359 use cases and judicious use of the locals() function in the
context of PEP 3150 (assuming the given clause semantics are exactly
as described by the implementation sketch in the PEP)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list