Trinary operator?

Grant Edwards grante at visi.com
Wed Apr 17 15:39:56 EDT 2002


In article <a9ki3d$jip$1 at nets3.rz.RWTH-Aachen.DE>, Philipp Lenssen wrote:
> "Philipp Lenssen" <lenssen at hitnet.rwth-aachen.de> wrote in message
> news:a9khoc$j7h$1 at nets3.rz.RWTH-Aachen.DE...
>>..
>> The three commented lines above would not have been needed if I'd use
>> something like '... name="' + (key != 'station') ? key : 'station' + '"
> ...'
>>..
> 
> That should be '... name="' + (key != 'station') ? key : 'lastStation' + '"
> ...'
> 
> Or since I sometimes prefer testing in a positive way rather than the most
> likey, '... name="' + (key == 'station') ? 'lastStation' : key + '" ...'

There's a dictionary lookup method called get() you can use
where you can specify the value to be returned if the key
doesn't exist. Using your previous example:

>>> shorthand = {'m':'male', 'f':'femail'}
>>> key = 'm'
>>> shorthand.get(key,key)
'male'
>>> key = 'whatever'
>>> shorthand.get(key,key)
'whatever'

-- 
Grant Edwards                   grante             Yow!  Nipples, dimples,
                                  at               knuckles, NICKLES,
                               visi.com            wrinkles, pimples!!



More information about the Python-list mailing list