[Tutor] beginning to code
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Wed Sep 13 00:58:56 EDT 2017
On Tue, 12 Sep 2017 20:01:52 -0700, Rick Johnson wrote:
> But just because we have been trained that the implicit `if x:` is
> shorthand for the reasonable `if bool(x) == True:`
That's not reasonable.
bool(x) already returns a True or False flag, comparing it to True is
superfluous. (Regardless of whether you use `is` or a slower equality
test.) It is excessively redundantly surplus.
And where do you stop? If you don't believe `bool(x)` is a flag, then you
can't believe `bool(x) == True` either, leading you into an infinite
regress of superfluous redundancy:
if (bool(x) == True) == True:
if ((bool(x) == True) == True) == True:
if (((bool(x) == True) == True) == True) == True:
if ((((bool(x) == True) == True) == True) == True) == True:
if (((((bool(x) == True) == True) == True) == True) == True) == True:
# help, I don't know how to stop!!!!
The *only* reasonable place to stop is right at the beginning:
if bool(x):
at least for languages like Pascal and Java where `if` requires a
specific boolean type.
And assuming x is not already a bool. If it is a bool, you of course
wouldn't redundantly call bool on it repeatedly again and again
redundantly:
if bool(bool(bool(bool(bool( ... (x)))))))...)
# help, I don't know where to stop!!!
If x is already a flag, then it would be silly to waste time calling bool
even once. You wouldn't write this:
a = int(25)
b = int(30)
c = int( (int(a) + int(b))*int(2) )
values = list([1, 2, 3])
x = list(values)[int(c)]
No, its quite obvious that anyone who would call bool() on something
which is already a bool, let alone the even more excessively superfluous
`if bool(x) is True`, is a cargo-cult programmer who isn't fluent enough
in the Python language to know what they're doing.
And of course the beauty of duck-typing in Python is that *everything* is
a bool, or at least quacks like a bool and swims like a bool.
[...]
> they're just reductio ad absurdum.
I see that your understanding of logical fallacies is as penetrating and
profound as your understanding of Python's design.
--
Steven D'Aprano
“You are deluded if you think software engineers who can't write
operating systems or applications without security holes, can write
virtualization layers without security holes.” —Theo de Raadt
More information about the Python-list
mailing list