[Tutor] beginning to code
Steve D'Aprano
steve+python at pearwood.info
Sun Sep 17 22:03:08 EDT 2017
On Mon, 18 Sep 2017 07:37 am, Rick Johnson wrote:
> Consider the collowing code:
>
> if bool(someObject) == True:
> # Do something
>
> Yes, from a "byte-code perspective", this source code is
> superfluous,
This is bad code because it is bad SOURCE CODE. The byte code is irrelevant.
You wouldn't write this:
if (len(mystring) == 1) == True:
because that's just stupid, the equality comparison returns a bool so testing
against True is redundant and adds no clarity, just noise. Nor would you write:
if (numchars > 2) == True:
because the greater than comparison returns a bool and so testing against True
is redundant and adds no clarity, just noise.
And you wouldn't write:
if (argument is None) == True:
because the `is` comparison returns a bool and testing it against True is
redundant and adds no clarity, just noise.
Nor would you write:
condition = some_string.startswith('x') == True
result = process(args, flag=(condition == True))
because in both lines you already have a bool and comparing a bool to True is
just redundant and adds noise.
And surely you wouldn't write:
results = sorted(values, reverse=(False==True))
Or would you? Maybe you would do all these things, in which case you're an even
worse programmer than I thought.
Your example of
if bool(someObject) == True:
is no different from any of these examples. bool(someObject) returns a bool;
comparing it to True adds redundancy and noise, not clarity.
Your insistence on adding the entirely superfluous, unnecessary and
distracting "== True" at the end of something which is already True or False
demonstrates a lack of fluency in the language and difficulty in reasoning
about boolean logic.
--
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