473,473 Members | 2,031 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Tertiary Operation

x = None
result = (x is None and "" or str(x))

print result, type(result)

---------------
OUTPUT
---------------
None <type 'str'>
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? I was expecting
"result" to be an empty string, not the str value of None.

Oct 17 '06 #1
10 2211

"abcd" <co*******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
>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 because the empty string
counts as false in a boolean context. Please learn to love the
new conditional expression syntax:

http://docs.python.org/whatsnew/pep-308.html
Oct 17 '06 #2
x = None
result = (x is None and "" or str(x))

print result, type(result)

---------------
OUTPUT
---------------
None <type 'str'>
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? I was expecting
"result" to be an empty string, not the str value of None.
An empty string evaluates to False, so it then continues to the
other branch. Either of the following should suffice:

# return a non-empty string
x is None and "None" or str(x)

# invert the logic and return
# something in the "and" portion
x is not None and str(x) or ""

There are more baroque ways of writing the terniary operator in
python (although I understand 2.5 or maybe python 3k should have
a true way of doing this). My understanding is that one common
solution is something like

{True: "", False: str(x)}[x is None]

-tkc

Oct 17 '06 #3
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 <type 'str'>
The "condition and result1 or result2" trick only works if result1 is an
expression with a True boolean value. The empty string has a false
boolean value.

You could force result1 to have a true boolean value by sticking it into
a list thusly:

result = (x is None and [""] or [str(x)])[0]

But that's ugly. Use Python 2.5 where there is a true conditional
expression or find another way to solve your problem.

-Carsten
Oct 17 '06 #4
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.

thanks.

Oct 17 '06 #5
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 as Y.

(x is None) evaluates as true, so (x is None and "") evaluates as "".
("") evaluates as false, so ("" or str(None)) evaluates as str(None).

The important factor you missed is, I think, that the empty string is
false in a boolean context.
>>if '':
.... print "empty string evaluates as true"
.... else:
.... print "empty string evaluates as false"
....
empty string evaluates as false

y = 5
result = (y is 5 and "it's five" or "it's not five")
(y is 5) evaluates as true, so (y is 5 and "it's five") evaluates as "it's
five".
"it's five" evaluates as true, so ("it's five" or ""it's not five")
evaluates as "it's five".
Your basic logic is okay, but you shouldn't test equality with "is".

== tests for equality;
is tests for object identity.

In the case of None, it is a singleton; every reference to None refers to
the same object. But integers like 5 aren't guaranteed to be singletons.
In your case, you were lucky that, by a fluke of implementation, "y is 5"
was true. But watch:
>>1+4 is 5
True
>>10001 + 10004 == 10005
True
>>10001 + 10004 is 10005
False

Always use == to test for equality, and (is) only to test for actual
object identity ("is this object the same as this one, not just two
objects with the same value?").
--
Steven.

Oct 17 '06 #6
In article <11**********************@i3g2000cwc.googlegroups. com>,
"abcd" <co*******@gmail.comwrote:
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.

thanks.
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. I know
about boolean short-circuit evaluation, and the and/or trick, but I still
look at

result = (x is not None and str(x) or "")

and have to puzzle through exactly what's going on there. You are going to
write your code once. It's going to be read many many times by different
people. It's worth a few more keystrokes on your part to save all those
future maintenance programmers headaches later.

You won't really understand just how important readability is until you're
maintaining a million lines of old code, most of it written by people who
are no longer on the project. I spend a lot of time staring at code
somebody else wrote and muttering things like, "What the **** does this
do?" That's just money down the toilet.

If I have to reach for a reference manual to look up operator binding rules
to understand a piece of code, that's bad. If I have to start making notes
on a piece of scrap paper, "Let's see, if x is this, then that's true, so
blah, blah", that's bad. If I look at something subtle, don't realize it's
subtle, and come to an incorrect conclusion about what it does, then base
some other decisions on that incorrect conclusion, that's really, really
bad.
Oct 17 '06 #7
abcd a écrit :
x = None
result = (x is None and "" or str(x))

print result, type(result)

---------------
OUTPUT
---------------
None <type 'str'>
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? I was expecting
"result" to be an empty string, not the str value of None.
the "<conditionand <if_trueor <if_false>" is NOT a ternary operator
but an ugly hack that breaks in some situations. It just happens that
you stumbled on one of those situations : <if_truemust never evaluate
as False. Please, do not use that ugly hack anymore and do a proper if
block.

Or use Python 2.5 with the official ternary operator ;)

Oct 17 '06 #8
abcd wrote:
x = None
result = (x is None and "" or str(x))
You don't need the parenthesis.
print result, type(result)

---------------
OUTPUT
---------------
None <type 'str'>
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 a context. Try with
100000 instead of 5:
>>x = 100000
x is 100000
False
>>>
print result

-------------
OUTPUT
-------------
it's five

...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.
As other already pointed, an empty string (as well as an empty list,
tuple, dict, set IIRC, and zero int or float) evals to False in a
boolean context.

Python 2.5 has a ternary operator. If you need to deal with older Python
versions, another possible 'ternary op hack' is :

x = None
(str(x), "")[x is None]
=""
x = 42
(str(x), "")[x is None]
="42"

This relies on the fact that False == 0 and True == 1.

NB : if you don't want to eval both terms before (which is how it should
be with a real ternanry operator), you can rewrite it like this:

result = (str, lambda obj:"")[x is None)(x)

But this begins to be unreadable enough to be replaced by a good old
if/else...

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Oct 17 '06 #9
abcd wrote:
x = None
result = (x is None and "" or str(x))
print result, type(result)

---------------
OUTPUT
---------------
None <type 'str'>

[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) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>x = None
'' if x is None else str(x)
''
>>>
Time to upgrade. ;-)

Steve
Oct 17 '06 #10
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:
return ""
return str(obj)

print tostring(key)
print tostring(foo), tostring(bar)
file.write(tostring(record[1]))

this also makes it easier to tweak things once you realize that not
everything should be passed through str():

def tostring(obj):
if obj is None:
return ""
if isinstance(obj, basestring):
return obj
return str(obj)

</F>

Oct 17 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Hardy Wang | last post by:
Hi, I have a win form application, when a button is clicked, a lengthy operation will be triggered. During the time program is still running, this application seems not to be able to response to...
0
by: John Jenkins | last post by:
Hi, any help on this would be greatly apprciated. I have been given a wsdl file from a customer generated by Oracle 10g Web Services tools. When I use wsdl.exe to attempt to produce proxy classes I...
0
by: relaxedrob | last post by:
Hi All, I have a portType such as this: <portType name="CMLeJobSoapGetEmpBrand"> <operation name="EJobGetEmpBrand"> <input message="tns:EJobEmpBrdReq" name="EJobEmpBrdReq"/> <output...
5
by: (PeteCresswell) | last post by:
User's screen has a "Sort By" option group on it with radio buttons for things like "Deal Name", "Collateral Manager", "Underwriter", Closing Date", "Holding Size", and so-on and so-forth. But...
2
by: Robinson | last post by:
I can start an Asynchronous operation against a data source with SQLCommand.BeginExecuteReader, allowing me to loop, checking for user cancellation before the operation has completed, but how then...
0
by: sms68 | last post by:
I need to create a date base using SMS for tertiary Institutions in Nigeria for a Government policy, How do i beging and what do. i need a system where a very graduate from a institution will send in...
10
by: Andrew Robinson | last post by:
I don't know if this is a compiler error or not. It all kind of makes sense but you would think that the compiler would check the types of the possible outcomes of the tertiary operator (c and d)...
8
by: Tyno Gendo | last post by:
I have just written a line of code like this (yes, many people think this stinks bad): <?php true == isset($page_seq) ? echo "$page_seq" : false; ?> However it breaks with 'Unexpected T_ECHO'...
0
by: Default User | last post by:
I work on creating test cases for a SOAP-based set of servers, using soapUI. I received and updated set of WSDL and schema files, and when I made new tests and mock server operations, all of the...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.