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
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
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
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
>
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
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
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
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
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
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
> 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
"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
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?
13 matches
Mail list logo