osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Tutor] beginning to code


Op 18-09-17 om 15:58 schreef Chris Angelico:
> On Mon, Sep 18, 2017 at 11:30 PM, Antoon Pardon <antoon.pardon at vub.be> wrote:
>
>> Well that you reduce an object to a boolean value is not obvious to
>> begin with. A TypeError because you are treating a non-boolean as
>> a boolean would have been more obvious to me.
> Sure, but those are basically your only two options. Either you have a
> way of interpreting some/all objects as booleans, or you require an
> explicit conversion. In Python syntax:
>
> # Interpret all objects as boolean:
> if x: pass
>
> # Require explicit conversion or comparison:
> if bool(x): pass
> if x == True: pass
>
> But not BOTH of the above:
>
> if bool(x) == True: pass
>
> That's pointless. It's not "more explicit". It's just more redundant.

But the problem is that the following two pieces of code don't do
the same in Python.

if x: pass
if x is True: pass

Sometimes I need that second statement but I can be sure that
should I show a piece of code on this mailing list with that
statement, all kind of remarks about it being redundant will
surface. The fact that x will be interpreted as a boolean
even if it isn't means that I need that second statement
when I want to do something only when the value of x is True.

-- 
Antoon Pardon.