Expression can be simplified on list

Steve D'Aprano steve+python at pearwood.info
Wed Sep 14 23:27:02 EDT 2016


On Thu, 15 Sep 2016 08:46 am, Lawrence D’Oliveiro wrote:

> On Thursday, September 15, 2016 at 9:12:25 AM UTC+12, Ned Batchelder
> wrote:
>>
>> On Wednesday, September 14, 2016 at 5:00:02 PM UTC-4, Lawrence D’Oliveiro
>> wrote:
>>>
>>> On Thursday, September 15, 2016 at 8:13:05 AM UTC+12, Terry Reedy wrote:
>>>>
>>>> Because True is the default, object need not and at least in CPython
>>>> does not have a __bool__ (or __len__) method.
>>> 
>>> If they had to (in the absence of which a bool() cast would not work),
>>> then that would help prevent problems like the one I previously linked
>>> to, don’t you think?
>> 
>> Requiring an explicit conversion method wouldn't help the "midnight is
>> False" problem: it was *caused* by an explicit conversion method.
> 
> “Conversion method” versus ”explicit conversion” are two different things.
> The class provided a method to convert to bool, but Python itself does
> automatic interpretation of non-boolean values as booleans. It is the
> latter I am objecting to.

So you have made clear, but what isn't clear is what is the reason for the
objection. Is it just a matter of taste?

If not, can you explain what *objective* reasons you have for preferring the
second over the first. Try to be explicit, rather than just dropping hints
and pretending that it is self-evident and obvious.


if obj: ...
# implicitly calls obj.__bool__ or equivalent, by the bool protocol

# versus

if bool(obj): ...
# implicitly calls obj.__bool__ or equivalent



Apart from the extra six characters, I see no difference. What am I missing?





-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list