Re: Some python comments/questions [ #1: "?:" equivalent ]

Steven D. Majewski (sdm7g@elvis.med.virginia.edu)
Tue, 12 Oct 1993 09:16:14 -0400

On Oct 11, 16:19, Bill Janssen wrote:
>
> I'm happier with python than with any scripting/extension language
> I've ever tried... but I have a few comments/questions:
>
> 1) I'm addicted to the C syntax of ?:, as in
>
> foo ((a < b) ? c : d);
>
> Is there some equivalent in python?
>

Having played around with trying to use and/or to force python into
a more C-like expression oriented pattern lately in my Obfuscated
Python posts, this comes to mind:

>>> ((( 1 < 2 ) and ( 'Less Than' ) or ( 'Not Less Than' )))
'Less Than'
>>> ((( 3 < 2 ) and ( 'Less Than' ) or ( 'Not Less Than' )))
'Not Less Than'

But I think the following is more python-ish:

>>> ( 'Not Less Than', 'Less Than' )[ ( 1 < 2 ) ]
'Less Than'
>>> ( 'Not Less Than', 'Less Than' )[ ( 3 < 2 ) ]
'Not Less Than'

i.e. using the boolean False = 0 / True = 1 to map into a
tuple index. BUT: since None and empty sequences are also
false and all others true, there is no guarantee that this
will work for all tests.

Predictions:
Tim will love it!
Guido *may* feel a bit ill at our continual efforts to make python
look like C !

- sdm