[Tutor] Change dictionary value depending on a conditional statement.
Norman Khine
norman at khine.net
Tue Feb 12 10:03:57 CET 2008
Thank you all, very nice.
Steve Willoughby wrote:
> Kent Johnson wrote:
>> Try
>> list.append({'id': 'name', 'link': ('YY','XX')[total > 0]})
>
> I'd caution against that, though. It's clever and cute, sure, but the
> meaning of it is obfuscated enough to be unpythonic because [total > 0]
> as a subscript doesn't mean anything unless you know you're taking
> advantage of an implementation detail that booleans are 0 for false and
> 1 for true. No matter how reliable that fact may be, I don't think that
> value should be stuck into a numeric context like that.
>
>> Or, in Python 2.5,
>> list.append({'id': 'name', 'link': ('XX' if total > 0 else 'YY')})
>
> This is much more clear, and would IMHO be fine.
>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list