Thank you Rob - I really appreciate your input.
Personally I like this more than the multiple-booleans option. We might be coming full circle here, but what if we accept a mode character instead of an enum member?
r → ..._reader()
w → ..._writer(mode='w')
x → ..._writer(mode='x')
a → ..._writer(mode='a')
r+ → ..._duplex(mode='r')
w+ → ..._duplex(mode='w')
PHP x+ → ..._duplex(mode='x')
PHP c → ..._writer(mode='c')
PHP c+ → ..._duplex(mode='c')
Using a string rather than an enum might not sit well in languages like Java, but I think in Python and in this case specifically (where other arguments like encoding and errors accept string arguments) it seems reasonably natural to me.
We’d document that _writer() accepts ‘w’, ‘x’, ‘a’ and ‘c’, and that _duplex() accepts ‘r’, ‘w’, ‘x’ and ‘c’. If we wanted to omit the PHP-specific modes to begin with, we could do that without painted ourselves into a corner.
From a user perspective, the open() algorithm is reasonably easy to grasp:
- If the mode includes ‘+’, remove it and call
_duplex(); otherwise - If the mode is ‘r’, call
_reader(); otherwise - If the mode is non-empty, call
_writer(); otherwise - Raise
ValueError
(I’m excluding handling of text/bytes and invalid modes for brevity)
What do you reckon?