Trinary operator?

Jeff Shannon jeff at ccvcorp.com
Fri Apr 19 13:33:59 EDT 2002


In article <ubvub71mto0d04 at news.supernews.com>, 
johnroth at ameritech.net says...
> 
> "Just van Rossum" <just at xs4all.nl> wrote in message
> news:just-4227D7.21004717042002 at news1.xs4all.nl...
> > In article <mailman.1019066614.29792.python-list at python.org>,
> >  "Mark McEahern" <marklists at mceahern.com> wrote:
> >
> > > > Not that it's a big deal since there's many ways to do that, but
> can I use
> > > > something like
> > > >     verboseGender = (gender == 'm') ? 'male' : 'female'
> > > > in Python?
> > >
> > > People seem to use and/or for that:
> >
> > But they shouldn't.
> 
> Why not? There's no other reason for the behavior
> of 'and' and 'or' returning the object that caused the
> final result.

I would say that they shouldn't do that because it's unclear, or 
at least unintuitive.  I know this trick exists, but any time I 
see 'a and b or c' I have a heck of a time remembering which gets 
returned in what case.  Of course, I never was thrilled about C's 
ternary operator, either.

I definitely think that using a small dictionary, even one 
created in-place (I.e., "{0:'female', 1:'male'}[gender=='m']"), 
is a more maintainable solution.  It still looks a bit ugly, but 
I can at least follow what's happening.

-- 

Jeff Shannon
Technician/Programmer
Credit International



More information about the Python-list mailing list