Any reason for this difference between python's readable() and writable() functions?

Viewed 30

When opening some file in Python 3.8.11, you get this unsurprising, boring behavior:

>>> f = open("notes.txt")
>>> f.writable()
False
>>> f.readable()
True

But when you close that file, the two functions behave differently:

>>> f.close()
>>> f.writable()
False
>>> f.readable()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file

I suppose there are really 2 things I don't understand. First, why throw an exception when trying to query about the state of a stream? Sure, it makes sense to throw an exception when trying to read() from a closed stream, but an exception while querying whether a stream is readable seems extreme. Second, if you take the attitude that exceptions are the proper way to handle a closed stream, why not be consistent? Shouldn't writable() throw an exception too?

Of course, the second part of my question may not have an answer beyond "They just wrote it that way", but I'm curious to see if there's some concrete reasoning there.

0 Answers
Related