osdir.com


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

[Tutor] beginning to code


Steven D'Aprano wrote:
> 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.

So what? That's a simple matter of translating source code
to byte code and has nothing to do with your inability to
grasp that byte code and source code are not a one-to-one
translation. Although the byte code and the source code of a
program carry the same _general_ information about how a
program will execute, the byte code is optimized for machine
communication whereas the source code is optimizes (or
should be!) for human communication. Byte code is meant to
be as concise and optimized as possible, whereas source code
is meant to be more elaborate. Humans are not machines, and
machines are not humans. We communicate in different ways.

Source code must be intuitive, and `if x` is not intuitive,
and if you don't believe me, then ask a non-programmer to
intuit what `if x` means. A neophyte may know enough about
basic logic to guess that the `if` is testing for some kind
True/False value, but the same noob will never guess that
the boolean value of an object can change based on such
abstract notions as "empty versus not empty" or "zero versus
more than zero". So, uhh, good luck with that little survey!

Your flaw is to judge the "if x" statement on the purely
abstract byte-compiled machine level, whereas i am judging
the statement on an intuitive human level. We can define
what happens on the abstract level all we want, but we
cannot define intuition. Neither can we define practicality.
Either a statement is intuitive, or it is not. Either an
action is practical, or it is not.

Do you also understand that we can translate *ANY* source
code into any byte code we want? There are no laws in the
universe that prevent Python from translating the source
code of "if bool(x) is True" to the same byte code that is
currently created by "if x". So stop lying, or prove that
such a translation is impossible.

> (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:
> 
> [snip: hyperbole]
> 
> 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.

Well, i should at least consider this a small victory.
Again, i must stress, that what Python sees is a matter of
the byte code translation, not a matter of what *WE*, as
programmers, see. So your argument is ignoring the real
issue.