(test) ? a:b

Michael Torrie torriem at gmail.com
Sat Oct 25 21:04:52 EDT 2014


On 10/22/2014 09:46 PM, Gregory Ewing wrote:
> Chris Angelico wrote:
>> I've seen much MUCH worse... where multiple conditional
>> expressions get combined arithmetically, and then the result used
>> somewhere...
> 
> In the days of old-school BASIC it was common to
> exploit the fact that boolean expressions evaluated
> to 0 or 1 (or -1, depending on your dialect :) to
> achieve conditional expressions or other tricks.

As far as I can tell, no BASIC dialect I've looked at (DOS and Linux
worlds only), has ever had any logical operators like AND (&&), OR (||),
and NOT (!).  They only appear to have bitwise operators (&,|,~ C
equivalent).  The fact that comparison operators returned 0 and -1 made
the bitwise operators function the same as logical.  But you can run
into trouble if you tried to use a common python idiom like this:

x = read_some_lines() 'returns number of lines read, or zero if none are
if not x:
	print ("I couldn't read any lines")
	exit(1)

Basically any value other than -1 for x would cause the not x to be
true.  Off topic, and no matter to python programmers.  But for BASIC
you need to watch out for this.  One BASIC dialect introduced a set of
functions, istrue() and isfalse() to convert a non-zero truth value to
-1, and any falsish value to zero (like maybe an empty string).  That
way the bitwise operators would always function as logical operators in
a conditional expression.





More information about the Python-list mailing list