[Tutor] beginning to code

Chris Angelico rosuav at gmail.com
Tue Sep 19 05:43:11 EDT 2017


On Tue, Sep 19, 2017 at 7:34 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> On Tue, 19 Sep 2017 17:46:32 +1000, Chris Angelico wrote:
>
>
>> # Display booleans differently if x is True:
>>     ... display flag
>> else:
>>     ... display number
>>
>> which would be better represented with "if isinstance(x, bool):"
>
> Given that True is a singleton, it is redundant to write
>
> if isinstance(x, bool) and x:
>
>
> I'd write "if x is True" if I really, honestly wanted True specifically.

Right, but if there's a different display for True, there's probably
also one for False.

if isinstance(x, bool):
    print("<input type=checkbox" + x * " checked" + " name=foo>Foo")
else:
    print("Foo <input name=foo value=%d>" % x)

But you're right that checking for True specifically is best done as
an identity check.

if x is True:
    print("[x] Foo")
elif x is False:
    print("[ ] Foo")
else:
    print("Foo:", x)

ChrisA



More information about the Python-list mailing list