From: Duncan Booth on
Albert van der Horst <albert(a)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)]

>>> map(lambda weight: STAMPS[bisect(WEIGHTS, weight)], [20, 100, 150, 999,
1000, 1100])
[44, 60, 60, 100, 120, 120]
From: Steven D'Aprano on
On Tue, 06 Apr 2010 16:54:18 +0000, Duncan Booth wrote:

> Albert van der Horst <albert(a)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?



--
Steven
From: Emile van Sebille on
On 4/6/2010 9:20 PM Steven D'Aprano said...
> On Tue, 06 Apr 2010 16:54:18 +0000, Duncan Booth wrote:
>> 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?
>

Would it be easier to digest as:

from bisect import bisect as selectindex #

WEIGHTLIMITS = [100, 250, 500, 1000]
POSTAGEAMOUNTS = [44, 60, 80, 100, 120]

postage = POSTAGEAMOUNTS[selectindex(WEIGHTLIMITS, weight)]

---

I've used bisect this way for some time -- I think Tim may have pointed
it out -- and it's been handy ever since.

Emile


From: Aahz on
In article <mailman.1330.1269981186.23598.python-list(a)python.org>,
Steve Holden <steve(a)holdenweb.com> wrote:
>
>It exists because people nagged Guido mercilessly until, against his
>better judgment, he capitulated.

No, the ternary exists because he became convinced that it was the
lesser evil compared with letting the abomination of

A and B or C

remain the "Pythonic" ternary and someone came up with a syntax that
wasn't completely painful.
--
Aahz (aahz(a)pythoncraft.com) <*> http://www.pythoncraft.com/

"It is easier to optimize correct code than to correct optimized code."
--Bill Harlan
From: Steven D'Aprano on
On Sun, 11 Apr 2010 21:35:49 -0700, Aahz wrote:

> In article <mailman.1330.1269981186.23598.python-list(a)python.org>, Steve
> Holden <steve(a)holdenweb.com> wrote:
>>
>>It exists because people nagged Guido mercilessly until, against his
>>better judgment, he capitulated.
>
> No, the ternary exists because he became convinced that it was the
> lesser evil compared with letting the abomination of
>
> A and B or C
>
> remain the "Pythonic" ternary and someone came up with a syntax that
> wasn't completely painful.



As I recall, Guido himself got bitten by a subtle bug in the A and B or C
idiom for ternary operator. Namely, if B is a false value, you get C even
if A is true.




--
Steven