<div dir="auto">In my code, I commonly use a NOT_SET singleton used as default value. I like this name for the test:<div dir="auto"><br></div><div dir="auto">if arg is NOT_SET: ...</div><div dir="auto"><br></div><div dir="auto">;-)</div><div dir="auto"><br></div><div dir="auto">I use that when I want to behave differently when None is passed. And yes, I have such code.</div><div dir="auto"><br></div><div dir="auto">Victor</div></div><div class="gmail_extra"><br><div class="gmail_quote">Le 2 mars 2017 9:36 AM, "M.-A. Lemburg" <<a href="mailto:mal@egenix.com">mal@egenix.com</a>> a écrit :<br type="attribution"><blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="elided-text">On 02.03.2017 09:03, Serhiy Storchaka wrote:<br>
> Function implemented in Python can have optional parameters with default<br>
> value. It also can accept arbitrary number of positional and keyword<br>
> arguments if use var-positional or var-keyword parameters (*args and<br>
> **kwargs). But there is no way to declare an optional parameter that<br>
> don't have default value. Currently you need to use the sentinel idiom<br>
> for implementing this:<br>
><br>
> _sentinel = object()<br>
> def get(store, key, default=_sentinel):<br>
> if store.exists(key):<br>
> return store.retrieve(key)<br>
> if default is _sentinel:<br>
> raise LookupError<br>
> else:<br>
> return default<br>
><br>
> There are drawback of this:<br>
><br>
> * Module's namespace is polluted with sentinel's variables.<br>
><br>
> * You need to check for the sentinel before passing it to other function<br>
> by accident.<br>
><br>
> * Possible name conflicts between sentinels for different functions of<br>
> the same module.<br>
><br>
> * Since the sentinel is accessible outside of the function, it possible<br>
> to pass it to the function.<br>
><br>
> * help() of the function shows reprs of default values. "foo(bar=<object<br>
> object at 0xb713c698>)" looks ugly.<br>
><br>
><br>
> I propose to add a new syntax for optional parameters. If the argument<br>
> corresponding to the optional parameter without default value is not<br>
> specified, the parameter takes no value. As well as the "*" prefix means<br>
> "arbitrary number of positional parameters", the prefix "?" can mean<br>
> "single optional parameter".<br>
><br>
> Example:<br>
><br>
> def get(store, key, ?default):<br>
> if store.exists(key):<br>
> return store.retrieve(key)<br>
> try:<br>
> return default<br>
> except NameError:<br>
> raise LookupError<br>
<br>
</div>Why a new syntax ? Can't we just have a pre-defined sentinel<br>
singleton NoDefault and use that throughout the code (and also<br>
special case it in argument parsing/handling)?<br>
<br>
def get(store, key, default=NoDefault):<br>
if store.exists(key):<br>
return store.retrieve(key)<br>
...<br>
<br>
I added a special singleton NotGiven to our mxTools long ago for<br>
this purpose.<br>
<br>
--<br>
Marc-Andre Lemburg<br>
eGenix.com<br>
<br>
Professional Python Services directly from the Experts (#1, Mar 02 2017)<br>
>>> Python Projects, Coaching and Consulting ... <a href="http://www.egenix.com/" rel="noreferrer" target="_blank">http://www.egenix.com/</a><br>
>>> Python Database Interfaces ... <a href="http://products.egenix.com/" rel="noreferrer" target="_blank">http://products.egenix.com/</a><br>
>>> Plone/Zope Database Interfaces ... <a href="http://zope.egenix.com/" rel="noreferrer" target="_blank">http://zope.egenix.com/</a><br>
______________________________<wbr>______________________________<wbr>____________<br>
<br>
::: We implement business ideas - efficiently in both time and costs :::<br>
<br>
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48<br>
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg<br>
Registered at Amtsgericht Duesseldorf: HRB 46611<br>
<a href="http://www.egenix.com/company/contact/" rel="noreferrer" target="_blank">http://www.egenix.com/company/<wbr>contact/</a><br>
<a href="http://www.malemburg.com/" rel="noreferrer" target="_blank">http://www.malemburg.com/</a><br>
<div class="elided-text"><br>
______________________________<wbr>_________________<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" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/<wbr>codeofconduct/</a><br>
</div></blockquote></div><br></div>