Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #101825

Re: Using 'Or'

From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: Using 'Or'
Date 2016-01-17 10:15 +1100
Message-ID <mailman.49.1452986139.15297.python-list@python.org> (permalink)
References <mailman.37.1452938481.15297.python-list@python.org> <48smy.82436$Dn6.27537@fx46.am4> <569abbf3$0$1603$c3e8da3$5496439d@news.astraweb.com> <mMzmy.46103$Yh.17821@fx36.am4>

Show all headers | View raw


On Sun, Jan 17, 2016 at 9:47 AM, Alister <alister.ware@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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Using 'Or' Kitten Corner <joshua.shapo@gmail.com> - 2016-01-15 15:24 -0500
  Re: Using 'Or' Christian Gollwitzer <auriocus@gmx.de> - 2016-01-16 12:03 +0100
    Re: Using 'Or' Marko Rauhamaa <marko@pacujo.net> - 2016-01-16 14:41 +0200
  Re: Using 'Or' Alister <alister.ware@ntlworld.com> - 2016-01-16 14:06 +0000
    Re: Using 'Or' Steven D'Aprano <steve@pearwood.info> - 2016-01-17 08:53 +1100
      Re: Using 'Or' Alister <alister.ware@ntlworld.com> - 2016-01-16 22:47 +0000
        Re: Using 'Or' Bernardo Sulzbach <mafagafogigante@gmail.com> - 2016-01-16 21:12 -0200
        Re: Using 'Or' Chris Angelico <rosuav@gmail.com> - 2016-01-17 10:15 +1100

csiph-web