one to many (passing variables)

Antoon Pardon antoon.pardon at rece.vub.ac.be
Wed Jul 30 07:27:15 EDT 2014


On 28-07-14 21:29, Terry Reedy wrote:

> On 7/25/2014 9:47 PM, C.D. Reimer wrote:
>> On 7/24/2014 2:58 AM, Ben Finney wrote:
>>> Here is an article on good API design; the principles apply to Python
>>> <URL:http://blog.isnotworking.com/2007/05/api-design-guidelines.html>.
>>> You know your API and its requirements better than we; see whether that
>>> sheds any light on improvements to make.
>> Thank you for the link. I'm curious about one item mentioned in the
>> article: "Avoid return values that Demand Exceptional Processing: return
>> zero-length array or empty collection, not null"
>> Isn't a zero-length array, empty collection and null all the same thing?
> No. [] is an empty list, None is a null.
>> Or does the "Demand Exceptional Processing" comes from testing to see if
>> the object is empty versus being null?
> Testing whether null or not.
>> And does this apply to Python?
> Yes. If a function always returns a iterable, sometimes empty, it can be 
> used as follows:
> for item in f(): process(item)
> If the iterable is empty, nothing happens.  If the function returns None 
> instead of empty, then the use has to write the following to get the 
> same result.

Taking this in consideration I think the io.RawIOBase.read got it backwards.

The documentation says the following:

| If 0 bytes are returned, and size was not 0, this indicates end of file.
| If the object is in non-blocking mode and no bytes are available, None is returned.

But typically if you are reading in non-blocking mode, no bytes availabe can be
treated as if you receive an empty (byte)string. While reaching the end of the
stream is different. So it would have been more consistent if an empty (byte)string
was return in case of no bytes availabe and None or io.EOF or something like that
in case of end of file.

Now I have to write things as follows:

for block in iter(partial(RawStream.read, 1024), ''):
    if block is not None:
        for b in block
            process(b)

Otherwise I could write it more as follows:

for block in iter(partial(RawStream.read, 1024), io.EOF):
    for b in block
        process(b)

-- 
Antoon Pardon




More information about the Python-list mailing list