[Python-ideas] Optional parameters without default value
M.-A. Lemburg
mal at egenix.com
Thu Mar 2 04:59:17 EST 2017
On 02.03.2017 09:45, Ivan Levkivskyi wrote:
> On 2 March 2017 at 09:36, M.-A. Lemburg <mal at egenix.com> wrote:
>
>> On 02.03.2017 09:03, Serhiy Storchaka wrote:
>>> Function implemented in Python can have optional parameters with default
>> [...]
>>
> Why a new syntax ? Can't we just have a pre-defined sentinel
>> singleton NoDefault and use that throughout the code (and also
>> special case it in argument parsing/handling)?
>>
>
> I think for the sane reason that we didn't add Undefined to PEP 484
> and PEP 526:
> Having "another kind of None" will cause code everywhere to expect it.
But that's exactly the point :-) Code should be made aware of
such a special value and act accordingly.
I had introduced NotGiven in our code to be able to differentiate
between having a parameter provided to a method/function or not,
and I needed a new singleton, because None was in fact a permitted
value for the parameters, but I still had to detect whether this
parameter was passed in or not.
Example:
>>> import mx.Tools
>>> mx.Tools.NotGiven
NotGiven
>>> def f(x=mx.Tools.NotGiven): pass
...
>>> help(f)
Help on function f in module __main__:
f(x=NotGiven)
Because it's a singleton, you can use "is" for test which is
very fast.
BTW: NotGiven was named after NotImplemented, another singleton
we have in Python. I had introduced this a long time ago to
implement better coercion logic:
http://web.archive.org/web/20011222024710/http://www.lemburg.com/files/python/CoercionProposal.html
--
Marc-Andre Lemburg
eGenix.com
Professional Python Services directly from the Experts (#1, Mar 02 2017)
>>> Python Projects, Coaching and Consulting ... http://www.egenix.com/
>>> Python Database Interfaces ... http://products.egenix.com/
>>> Plone/Zope Database Interfaces ... http://zope.egenix.com/
________________________________________________________________________
::: We implement business ideas - efficiently in both time and costs :::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
http://www.egenix.com/company/contact/
http://www.malemburg.com/
More information about the Python-ideas
mailing list