[Python-ideas] Better repr() for sentinel objects
Chris Angelico
rosuav at gmail.com
Sat Feb 15 14:54:41 CET 2014
On Sat, Feb 15, 2014 at 11:37 PM, Michael Foord <fuzzyman at gmail.com> wrote:
> There's always this:
>
>>>> from unittest.mock import sentinel
>>>> sentinel.FOO
> sentinel.FOO
Looks reasonable. The implementation is pretty much the same as I had,
with the additional feature of the object with __getattr__ that
generates and caches them. I wouldn't like to depend on unittest, but
if that could be lifted out and put into a more plausible place (and
then unittest.mock could import it from there, which would mean that
nothing visible would change), it'd work. Downside: The names must be
unique, so they can't be as descriptive. (If two functions want to use
a "No timeout" sentinel, they'd either have to go for unique names, or
be sharing a sentinel, which is probably not a good thing.)
I suppose the question is: Is it a good thing for callers to be able
to invoke the default? Currently, urllib.request.urlopen consciously
calls up the default-sentinel for socket.create_connection; if that
were done as "sentinel.NO_TIMEOUT", then by definition everyone who
types that *will* get the effect of omitting the argument. This is a
significantly bigger change than simply "hey wouldn't it be nice if
the repr() were more readable", though.
ChrisA
More information about the Python-ideas
mailing list