(a==b) ? 'Yes' : 'No'
Duncan Booth
duncan.booth at invalid.invalid
Wed Apr 7 04:15:38 EDT 2010
Steven D'Aprano <steven at REMOVE.THIS.cybersource.com.au> wrote:
> On Tue, 06 Apr 2010 16:54:18 +0000, Duncan Booth wrote:
>
>> Albert van der Horst <albert at spenarnc.xs4all.nl> wrote:
>>
>>> Old hands would have ...
>>> stamp =( weight>=1000 and 120 or
>>> weight>=500 and 100 or
>>> weight>=250 and 80 or
>>> weight>=100 and 60 or
>>> 44 )
>>>
>>> (Kind of a brain twister, I think, inferior to C, once the c-construct
>>> is accepted as idiomatic.)
>>
>> I doubt many old hands would try to join multiple and/or operators that
>> way. Most old hands would (IMHO) write the if statements out in full,
>> though some might remember that Python comes 'batteries included':
>>
>> from bisect import bisect
>> WEIGHTS = [100, 250, 500, 1000]
>> STAMPS = [44, 60, 80, 100, 120]
>>
>> ...
>> stamp = STAMPS[bisect(WEIGHTS,weight)]
>
>
> Isn't that an awfully heavyweight and obfuscated solution for choosing
> between five options? Fifty-five options, absolutely, but five?
>
I did say most people would simply write out an if statement.
However, since you ask, using bisect here allows you to separate the data
from the code and even with only 5 values that may be worthwhile.
Especially if there's any risk it could become 6 next week.
--
Duncan Booth http://kupuguy.blogspot.com
More information about the Python-list
mailing list