thp at cs.ucr.edu wrote:

Terry Reedy wrote:

+ "daniel w. moore" <dan65536 at hotmail.com> wrote in message

+ news:33b491f.0209181820.17b6ea70 at posting.google.com...

+> It's occurred to me after some brainstorming on the subject that the

+> C/perl construct

+>

+> a ? b : c

+>

+> can be arranged like this in python:

+>

+> (a and [b] or [c])[0]

+ If you *know* that b will evaluate as True (perhaps because it is a

+ non-null constant, then this can be simplified to 'a and b or c'. I

+ first used this several years ago.

But why use grotesque hacks in the first place? "a ? b : c" is a

well-established syntax for conditional expressions. Why doesn't

Python simply adopt it?

Tom Payne

Terry Reedy wrote:

+ "daniel w. moore" <dan65536 at hotmail.com> wrote in message

+ news:33b491f.0209181820.17b6ea70 at posting.google.com...

+> It's occurred to me after some brainstorming on the subject that the

+> C/perl construct

+>

+> a ? b : c

+>

+> can be arranged like this in python:

+>

+> (a and [b] or [c])[0]

+ If you *know* that b will evaluate as True (perhaps because it is a

+ non-null constant, then this can be simplified to 'a and b or c'. I

+ first used this several years ago.

But why use grotesque hacks in the first place? "a ? b : c" is a

well-established syntax for conditional expressions. Why doesn't

Python simply adopt it?

Tom Payne

I assume that there are reasons why it could clutter or make ambiguous

the syntax - but I'm not expert.

I find the (a and [b] or [c])[0] construct to obscure to be useful - the

space it saves is wasted by the comment I have to put in to explain it.

It occurs to me that the bool() built in function allows for what is -

in my opinion - a cleaner solution. Easier for mortals to comprehend.

Basically, construct a two entry list of the two items and then index

that with your expression, converted to bool. The bool function is

essential since there was previously no generic way of getting the

true/false value of an expression.

For example:

Python 2.2.1 (#34, Sep 14 2002, 09:40:33) [MSC 32 bit (Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.

>>>

a = "Hello"

b = "Goodbye"

myString = ""

[a,b][bool(myString)]

b = "Goodbye"

myString = ""

[a,b][bool(myString)]

myString = "Something"

[a,b][bool(myString)]

[a,b][bool(myString)]