[Tutor] toggle idiom?

dman dman@dman.ddts.net
Thu, 9 May 2002 09:01:32 -0500


--MGYHOYXEY6WxJCY8
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Thu, May 09, 2002 at 07:50:37AM -0400, Erik Price wrote:
|=20
| On Thursday, May 9, 2002, at 12:12  AM, dman wrote:
|=20
| >Old code would only break when it uses the boolean values in a
| >non-boolean context or when checking equality of two "boolean" values.
| >For example the following snippets :
| >
| >if a and b : print "good"
| >if a =3D=3D b : print "not good"
| >
| >The former will not break as long as the truth value of the objects
| >remains the same (which is independent from "True=3D=3D1").  It can even
| >handle the situation where different "true" values are used for a and
| >b.  The second expression would break if a and b were different
| >objects both evaluating to true.
|=20
| Won't the former also print "good" if for some reason the value of a is=
=20
| 1 and the value of b is 2?

Yes, because both are "true".

| This could be problematic too, no?

*IF* you meant to perform an arithmetic operation.  The original block
of code contained only a boolean test, though the result of the test
is ambiguous as to whether the result is boolean or arithmetic (which
is a major reason for python 2.3 including the names True and False).

| Or do we just have to trust that this test is ONLY being used for
| boolean value, and that we aren't checking for isequals or any other
| value test....

Right.  If you want to work in the realm of logic, use logical
operators and values (eg objectes named True and False).  If you want
to work in the realm of arithmetic, then use arithmetic values (ints,
floats) and not boolean ones.  Intermixing the two may be fun when
coming up with the most obfuscated way of writing something, but not a
good idea when writing real code.  (eg don't use exponentiation to
achieve a logic operation :-))

-D

--=20

Like a gold ring in a pig's snout
is a beautiful woman who shows no discretion.
        Proverbs 11:22
=20
GnuPG key : http://dman.ddts.net/~dman/public_key.gpg


--MGYHOYXEY6WxJCY8
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iEYEARECAAYFAjzagTwACgkQO8l8XBKTpRTI6gCgqbRF6azcKb7mMWU+ZAWFlGNu
iCMAoMoL0HYGPzAtxCzCC5EGiSXOz352
=ez+S
-----END PGP SIGNATURE-----

--MGYHOYXEY6WxJCY8--