Using 'Or'

Chris Angelico rosuav at gmail.com
Sat Jan 16 18:15:29 EST 2016


On Sun, Jan 17, 2016 at 9:47 AM, Alister <alister.ware at ntlworld.com> wrote:
> On 16/01/16 21:53, Steven D'Aprano wrote:
>>
>> On Sun, 17 Jan 2016 01:06 am, Alister wrote:
>>
>>> Conditional operators (or and not == etc.) need to be used in a test
>>
>>
>> Technically, that is incorrect.
>
> yes but the op is confused in his usage enough at present

Adding a falsehood sometimes helps reduce the confusion, but in this
case it just worsens things, I think.

>>> how else would you expect you print statement to be able to decided
>>> which to print?
>>
>>
>>
>> default = "I like Brussels sprouts."
>> message = random.choice(["", "I like boiled cabbage."])
>> print( message or default )
>>
>>
>>
> I hope I never see production code like that

Why? Okay, maybe not with a random.choice, but what about dict lookup?

specific_messages = {
     "foo": "You use a new foo.",
    "bar": "You sing a few bars of music.",
}
print(specific_messages.get(kwd) or "You {} vehemently.".format(kwd))

Seems fine to me.

ChrisA



More information about the Python-list mailing list