[Python-Dev] Re: Re: Trinary Operators
Jp Calderone
exarkun@intarweb.us
Fri, 7 Feb 2003 02:11:00 -0500
--GvXjxJ+pjyke8COw
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
On Thu, Feb 06, 2003 at 09:43:40PM -0500, Terry Reedy wrote:
>=20
> [snip]
>=20
> I suspect that careful use of and/or will take care of half of real
> use cases for ternary (not 'trinary') operators. IE, at least one of
> the expressions is either a non-null constant, as above, or an
> expression known to never be null when its condition is true.
A semi-common case I have in my code is this:
class Foo:
def __init__(self, x =3D None):
if x is None:
x =3D []
self.x =3D x
About half the time, I start to write the body of __init__ as this,
though:
self.x =3D x is not None and x or []
And then kick myself, for it potentially fails or produces unexpected
behavior when something passes [] into the function, and a reference to
*that* empty list is kept by the caller. It might be convenient to be able
to write:
self.x =3D [] if x is None else x
So, this is a case where both expressions may evaluate to false, but the
identity of which is used as the final result is significant.
otoh-I'm-happy-with-the-current-form'ly, Jp
--=20
#!/bin/bash
( LIST=3D(~/.netscape/sigs/*.sig)
cat ${LIST[$(($RANDOM % ${#LIST[*]}))]}
echo --$'\n' `uptime` ) > ~/.netscape/.signature
--=20
up 53 days, 11:50, 8 users, load average: 0.13, 0.22, 0.19
--GvXjxJ+pjyke8COw
Content-Type: application/pgp-signature
Content-Disposition: inline
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.0 (GNU/Linux)
iD8DBQE+Q1wEedcO2BJA+4YRAncpAKDT69kGkZvrLGliBp9Jt3mRHujPIQCfcCeO
ly1TglRBJbL3pVEx36ZCddg=
=xc23
-----END PGP SIGNATURE-----
--GvXjxJ+pjyke8COw--