[Tutor] beginning to code
Op 18-09-17 om 14:58 schreef Chris Angelico:
> On Mon, Sep 18, 2017 at 10:42 PM, Rick Johnson
> <rantingrickjohnson at gmail.com> wrote:
>> (2) Python is, in essence, converting the source code
>> syntax of:
>> if someObject:
>> if BOOLEAN_VALUE_OF_SOMEOBJECT:
>> Which, although the result of such a conversion (a
>> Boolean) will be perfectly symmetrical with formal logic
>> statements, the conversion is not obvious, because it
>> happens behind the curtains.
> What a surprise. In the context of a statement that has exactly two
> possible courses of action (either you go into the 'if' block, or you
> go into the 'else' block (if any)), you use the boolean value of an
> object. What part of this is not obvious?
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.
A second thought is that it isn't obvious that empty strings, lists ...
should be thought of as falsy. Sometimes I am treating a stream of
values/objects and when I ask for the next available items, i get
a string/list. An empty string/list in that context would mean that
nothing was available, but it is possible that more will be available
later and so could be treated just like a non-empty list/string.
But then, I can't mix this kind of interface with the io module because
a read from classes in that module will produce an empty (byte)string
to indicate the end of the stream.
Once you understand how python does things, you can work with it, but
IMO the way python does things is IMO not at all that obvious as people
would like us to think.