Status of side-effecting functions in python

Marko Rauhamaa marko at pacujo.net
Sun Oct 26 12:50:58 EDT 2014


wxjmfauth at gmail.com:

> Yes and no. If something goes wrong in a .write() method,
> is not Python supposed to raise an error? (!)

We have multiple cases:

 1. write succeeds with all of the given bytes

 2. write succeeds with some but not all of the given bytes

 3. write cannot at the moment write any bytes

 4. an I/O error has taken place

Cases 1 and 2 are reported through positive return values in POSIX
system calls. Cases 3 and 4 are reported as errors. Arguably 2 and 3 are
related cases.

Python imitates POSIX, but *could* take a different tack. For example,

 * always return with an exception carrying the number of bytes written

 * return with None in case 1 and with an exception otherwise

 * return with the number of bytes in 1, 2 and 3 and exception in 4

The POSIX way is justified with symmetry: read() distinguishes EAGAIN
from 0 (end of file). One could argue that POSIX got it the wrong way.
EOF should be an exception ("EEOF") and 0 should be used instead of
EAGAIN. However, the POSIX practice works in C and Python.


Marko



More information about the Python-list mailing list