python3: accessing the result of 'if'

Bengt Richter bokr at oz.net
Sun Jan 9 15:45:23 EST 2005


On Sat, 08 Jan 2005 18:30:25 +1000, Nick Coghlan <ncoghlan at iinet.net.au> wrote:

>Carl Banks wrote:
>> Right.  But you know that as soon as you add this to simple
>> expressions, a bunch of people are going to come here whining about how
>> they don't get to use where with if-expressions.
So why shouldn't they get to?

>> 
>> Frankly, they might have a point here.  Although we have replacing
>> lambda expressions on our minds, I have in mind a different problem
>> that a where-statement would solve perfectly.  But it would have to be
>> used with an if-expression.
>
>I have a different suggestion for this.
>
>'as' is used for renaming in import statements. 'as' will be used for exception 
>naming in Python 3k.
>
>So let's use it for expression naming in 'if' statements, too.
>
>if someregexp.match(s) as m:
>   # blah using m
>elif someotherregexp.match(s) as m:
>   # blah using m
>

If 'where: ...' were an expression suite ISTM you could write

 if m where: m = someregexp.match(s)  # same-line format
    blah(m)
 elif m where:
        m = someotherregexp(s) # indented suite format
    blah(m) # dedent from where-suite starts elif-suite

Regards,
Bengt Richter



More information about the Python-list mailing list