Re: Tertiary Operation

2006-10-17 Thread Fredrik Lundh
Roy Smith wrote: > Why not just: > > if x is None: >result = str(x) > else: >result = "" > > It's a couple more lines of code, but it's obvious what it means. and if you're doing this a few times, putting it in a function is even better. def tostring(obj): if obj is None

Re: Tertiary Operation

2006-10-17 Thread Steven Bethard
abcd wrote: > x = None > result = (x is None and "" or str(x)) > print result, type(result) > > --- > OUTPUT > --- > None > > [snip] > ...what's wrong with the first operation I did with x? You weren't using Python 2.5: Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MS

Re: Tertiary Operation

2006-10-17 Thread Bruno Desthuilliers
abcd wrote: > x = None > result = (x is None and "" or str(x)) You don't need the parenthesis. > print result, type(result) > > --- > OUTPUT > --- > None > > > y = 5 > result = (y is 5 and "it's five" or "it's not five") By all means *don't* use identity tests in such

Re: Tertiary Operation

2006-10-17 Thread Christophe
abcd a écrit : > x = None > result = (x is None and "" or str(x)) > > print result, type(result) > > --- > OUTPUT > --- > None > > > y = 5 > result = (y is 5 and "it's five" or "it's not five") > > print result > > - > OUTPUT > - > it's five >

Re: Tertiary Operation

2006-10-17 Thread Roy Smith
In article <[EMAIL PROTECTED]>, "abcd" <[EMAIL PROTECTED]> wrote: > Carsten Haese wrote: > > Use Python 2.5 where there is a true conditional > > expression or find another way to solve your problem. > > python 2.5 once we upgrade (hopefully soon), anyways...an earlier post > suggested the inver

Re: Tertiary Operation

2006-10-17 Thread Steven D'Aprano
On Tue, 17 Oct 2006 06:30:32 -0700, abcd wrote: > x = None > result = (x is None and "" or str(x)) Boolean operators "and" and "or" stop as soon as a result is known. So: X and Y evaluates as X if X is false; otherwise it evaluates as Y. X or Y evaluates as X if X is true; otherwise it evaluates

Re: Tertiary Operation

2006-10-17 Thread Carsten Haese
On Tue, 2006-10-17 at 09:48, Tim Chase wrote: > [...] > Either of the following should suffice: > > # return a non-empty string > x is None and "None" or str(x) This one can be "optimized" to just str(x) since str(None)=="None". >[...] > There are more baroque ways of writing the ter

Re: Tertiary Operation

2006-10-17 Thread Fredrik Lundh
Tim Chase wrote: > {True: "", False: str(x)}[x is None] first the localtime/mktime roundtrip with a simulated cast, and now this? in less than one hour? solar flares? -- http://mail.python.org/mailman/listinfo/python-list

Re: Tertiary Operation

2006-10-17 Thread abcd
Carsten Haese wrote: > Use Python 2.5 where there is a true conditional > expression or find another way to solve your problem. python 2.5 once we upgrade (hopefully soon), anyways...an earlier post suggested the inverse... x = None result = (x is not None and str(x) or "") which works just fine

Re: Tertiary Operation

2006-10-17 Thread Carsten Haese
On Tue, 2006-10-17 at 09:30, abcd wrote: > x = None > result = (x is None and "" or str(x)) > > print result, type(result) > > --- > OUTPUT > --- > None The "condition and result1 or result2" trick only works if result1 is an expression with a True boolean value. The emp

Re: Tertiary Operation

2006-10-17 Thread Tim Chase
> x = None > result = (x is None and "" or str(x)) > > print result, type(result) > > --- > OUTPUT > --- > None > > > y = 5 > result = (y is 5 and "it's five" or "it's not five") > > print result > > - > OUTPUT > - > it's five > > ...what's wr

Re: Tertiary Operation

2006-10-17 Thread Richard Brodie
"abcd" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] >x = None > result = (x is None and "" or str(x)) > > ...what's wrong with the first operation I did with x? I was expecting > "result" to be an empty string, not the str value of None. Your evil tertiary hack has failed you be

Tertiary Operation

2006-10-17 Thread abcd
x = None result = (x is None and "" or str(x)) print result, type(result) --- OUTPUT --- None y = 5 result = (y is 5 and "it's five" or "it's not five") print result - OUTPUT - it's five ...what's wrong with the first operation I did with x?