<div dir="ltr">A class like this is useful although I've called it NamedObject (by analogy to NamedTuple). It's useful in contexts other than as a sentinel, for example to represent deleted values, but the fundamental aspects are that it's a unique object that has a name.<div>
<br></div><div>I've done two different things in my case:</div><div><br></div><div>(1) add __setattr__ so you can't set values on this object (to prevent someone that gets the object from operating on it as if it's some other object).</div>
<div><br></div><div>(2) printing str(descr) not repr(descr). Since the standard use case is passing a string to the constructor, it's not valuable for it to print quotes around it. I also use <> instead of NamedObject() but I don't care about that distinction.</div>
<div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><font face="courier new, monospace">>>> a = NamedObject('DELETED'), NamedObject('DELETED')</font></div><div>
<font face="courier new, monospace">>>> a</font></div><div><font face="courier new, monospace">(<DELETED>, <DELETED>)</font></div><div><font face="courier new, monospace">>>> a[0] == a[1]</font></div>
<div><font face="courier new, monospace">False</font></div></blockquote><div><br></div><div>Note the last line: this is different from how unittest.mock.sentinel works. I find both cases useful (and particularly find the unittest.mock.sentinel version more useful in unittests).</div>
<div><br></div><div>Writing this up I suppose UniqueObject might be a better name for this than NamedObject.</div></div><div class="gmail_extra"><br clear="all"><div><div dir="ltr"><font face="arial, helvetica, sans-serif">--- Bruce<br>
</font><div><div><font face="arial, helvetica, sans-serif">Learn how hackers think: <a href="http://j.mp/gruyere-security" target="_blank">http://j.mp/gruyere-security</a></font></div></div></div></div>
<br><br><div class="gmail_quote">On Sat, Feb 15, 2014 at 3:56 AM, Chris Angelico <span dir="ltr"><<a href="mailto:rosuav@gmail.com" target="_blank">rosuav@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
A common pattern, both in the stdlib and in user code, is the<br>
dedicated sentinel object to recognize an omitted argument:<br>
<br>
no_timeout = object()<br>
def get_data(source, timeout=no_timeout):<br>
if timeout is not no_timeout:<br>
source.set_alarm(timeout)<br>
return source.pop()<br>
<br>
This is a bit unclear in the function signature, as seen in help():<br>
<br>
"""<br>
Help on function get_data in module __main__:<br>
<br>
get_data(source, timeout=<object object at 0x7f579fe53070>)<br>
"""<br>
<br>
The stdlib does this:<br>
"""<br>
Help on function create_connection in module socket:<br>
<br>
create_connection(address, timeout=<object object at 0x7fe52900b080>,<br>
source_address=None)<br>
""" # chomp docstring<br>
<br>
"""<br>
Help on function urlopen in module urllib.request:<br>
<br>
urlopen(url, data=None, timeout=<object object at 0x7fe52900b080>, *,<br>
cafile=None, capath=None, cadefault=False)<br>
""" # no docstring to chomp, actually<br>
<br>
It's not particularly useful to get the object's address. Proposal: A<br>
self-documenting Sentinel class which serves the exact same purpose.<br>
<br>
class Sentinel:<br>
def __init__(self, desc):<br>
self.desc = desc<br>
def __repr__(self):<br>
return "Sentinel(" + repr(self.desc) + ")"<br>
<br>
This can then be used just like object(), only it retains something<br>
for the benefit of its repr:<br>
<br>
no_timeout = Sentinel("No timeout")<br>
# Same function definition<br>
"""<br>
Help on function get_data in module __main__:<br>
<br>
get_data(source, timeout=Sentinel('No timeout'))<br>
"""<br>
<br>
I don't know how this interacts with Argument Clinic and C-written<br>
functions. If there's something that's being done for those that would<br>
make sentinels come out a particular way in their help() info, ideally<br>
this should be displayed the same way (or at least similarly).<br>
<br>
Thoughts?<br>
<br>
ChrisA<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div><br></div>