while c = f.read(1)

Antoon Pardon apardon at forel.vub.ac.be
Mon Aug 22 04:41:13 EDT 2005


Op 2005-08-19, Donn Cave schreef <donn at u.washington.edu>:
> In article <slrndgbole.209.apardon at rcpc42.vub.ac.be>,
>  Antoon Pardon <apardon at forel.vub.ac.be> wrote:
> ...
>> But '', {}, [] and () are not nothing. They are empty containers.
>
> Oh come on, "empty" is all about nothing.

No it is not. There are situation where False or None should
be treated differently from an empty sequence.

Empty can mean, "nothing yet" which should be treated
differently from "nothomg more".

>> And 0 is not nothing either it is a number. Suppose I have
>> a variable that is either None if I'm not registered and a
>> registration number if I am. In this case 0 should be treated
>> as any other number.
>> 
>> Such possibilities, make me shy away from just using 'nothing'
>> as false and writing out my conditionals more explicitly.
>
> Sure, if your function's type is "None | int", then certainly
> you must explicitly check for None.

The fact is that python doesn't know which type a function is.
So why does python guess that zero should be treated as false.

> That is not the case with
> fileobject read(), nor with many functions in Python that
> reasonably and ideally return a value of a type that may
> meaningfully test false.  In this case, comparison (==) with
> the false value ('') is silly.

No is is not. The comparison with the specific false value
makes it easier for the reader of the code to find out what
to expect. I also find the use of '' as false in this context
wrong. A read can be used on all kind of objects including
a network connection. Returning '' on a network read would
be IMO the most natural answer to say, the network connection
is still open but no data is available for the moment. '' here
would mean "nothing yet" while '' is now made into "nothing more"

-- 
Antoon Pardon



More information about the Python-list mailing list