[Tutor] toggle idiom?
Paul Sidorsky
paulsid@shaw.ca
Wed, 08 May 2002 17:22:38 -0600
Sean 'Shaleh' Perry wrote:
> > is the best way of writing the original code because it clearly
> > shows the logic (as apposed to arithmetic) intent of the operation and
> > works for all cases the original works for. The only reason for using
> > the original instead of this is if a must be 0 or 1 in the end, rather
> > than simply a false or true value.
>
> is there a case where 'a = not a' will yield a value other than 0 or 1? i am
> not aware of one.
Neither am I, and furthermore the new boolean type coming in for 2.3
will have True == 1 and False == 0. This strongly suggests that not
False == True == 1 is already the norm, as is not <nonzero> == False ==
0. If this wasn't the case then old code could break when the boolean
type is introduced, and avoiding that was a key concern (see PEP 285).
I can't see any problem with a = not a. It's been used in C for years,
perhaps decades. In fact the only other reasonable alternative I've
seen is using C's ternary ?: operator (a = a ? 0 : 1) which is really
just a short form of what the OP already had, but since Python has no ?:
or equivalent this isn't an option.
--
======================================================================
Paul Sidorsky Calgary, Canada
paulsid@shaw.ca http://members.shaw.ca/paulsid/