python3: accessing the result of 'if'

Carl Banks invalidemail at aerojockey.com
Sun Jan 9 00:44:47 EST 2005


Nick Coghlan wrote:
> Carl Banks wrote:
> > What if the condition you wanted to test wasn't the same as the
thing
> > you want to save?  In other words, how would you convert this?
> >
> > . where:
> > .     m = something()
> > . if m > 20:
> > .     do_something_with(m)
>
> Yeah, this problem eventually occurred to me as well. However, I
think a little
> utility function can help solve it:
>
>    def test(val, condition):
>      if condition(val):
>        return val
>       else:
>        return None
>
>    if test(something(), lambda x: x < 10) as m:
>      print "Case 1:", m
>    elif test(something(), lambda x: x > 20) as m:
>      print "Case 2:", m
>    else:
>      print "No case at all!"

I'm sorry, I really can't agree that this helper function "solves" it.
IMO, it's a workaround, not a solution.  And, if I may be frank, it's a
pretty ugly one.

Not only that, but it still doesn't work.  What if the object itself is
false?


-- 
CARL BANKS




More information about the Python-list mailing list