[Python-ideas] Optional parameters without default value
M.-A. Lemburg
mal at egenix.com
Thu Mar 2 07:08:42 EST 2017
On 02.03.2017 12:31, Stephan Houben wrote:
> I am not sure if I fully understand the proposal then.
>
> NoDefault would be special syntax so that this would be disallowed:
>
> f(NoDefault)
>
> but this would be allowed:
> def f(x=NoDefault):
> ...
>
> and also this:
>
> x is NoDefault
>
> So this would seem to require an exhaustive list of syntactic contexts
> in which NoDefault is allowed. I mean, can I do:
>
> x = NoDefault
>
> ?
>
> I observe that I can always get to the underlying NoDefault object in this
> way:
>
> (lambda x=NoDefault:x)()
>
> So what happens if I do:
>
> f((lambda x=NoDefault:x)())
>
> ?
Sorry for the confusion. NoDefault would be usable just like
any other singleton.
There would only be one case where it would cause an exception,
namely when you declare a parameter as having NoDefault as value.
This would trigger special logic in the argument parsing code to
disallow using that parameter as keyword parameter.
Example:
def f(x=NoDefault):
# x is an optional positional parameter
if x is NoDefault:
# x was not passed in as parameter
...
else:
# x was provided as parameter
...
These would all work fine:
f()
f(1)
f(None)
This would trigger an exception in the argument parsing code:
f(x=NoDefault)
e.g. TypeError('x is a positional only parameter')
This would not trigger an exception:
f(NoDefault)
since x is not being used as keyword parameter and the
function f may want to pass the optional positional parameter
down to other functions with optional positional paramters
as well.
Is this clearer now ?
Note: The name of the singleton could be something else
as well, e.g. NoKeywordParameter :-)
> Stephan
>
>
> 2017-03-02 12:15 GMT+01:00 M.-A. Lemburg <mal at egenix.com>:
>
>> On 02.03.2017 11:22, Stephan Houben wrote:
>>> In cases like this I would recommend creating the sentinel yourself:
>>>
>>> NoDefault = object()
>>>
>>> def get(store, key, default=NoDefault):
>>> if default is NoDefault:
>>> # do something
>>>
>>> You can arrange to not export NoDefault so that the client code cannot
>> even
>>> access
>>> the sentinel value.
>>
>> Yes, I know... I've been using the mxTools NotGiven since 1998.
>>
>>> This is strictly preferable over having yet another global
>>> value meaning "no value", since that just moves the goal posts:
>>> clients will complain they cannot pass in a default=NoDefault and get
>> back
>>> NoDefault.
>>
>> Not really. NoDefault would mean: no value provided, not that
>> you don't want a value. As a result, passing NoDefault would
>> not be allowed, since then you'd be providing a value :-)
>>
>>> Stephan
>>>
>>>
>>> 2017-03-02 11:04 GMT+01:00 M.-A. Lemburg <mal at egenix.com>:
>>>
>>>> On 02.03.2017 10:06, Serhiy Storchaka wrote:
>>>>> On 02.03.17 10:36, M.-A. Lemburg wrote:
>>>>>> 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)?
>>>>>>
>>>>>> def get(store, key, default=NoDefault):
>>>>>> if store.exists(key):
>>>>>> return store.retrieve(key)
>>>>>> ...
>>>>>
>>>>> This means adding a new syntax. NoDefault should be a keyword (we can
>>>>> reuse existing keyword couldn't be used in expression), and it should
>> be
>>>>> accepted only in the specific context of declaring function parameter.
>>>>
>>>> This is not new syntax, nor is it a keyword. It's only a
>>>> new singleton and it is well usable outside of function
>>>> declarations as well, e.g. for class attributes which are
>>>> not yet initialized (and which can accept None as value).
>>>>
>>>> The only special casing would be in function call
>>>> parameter parsing to signal errors when the parameter
>>>> is used as keyword parameter.
>>>>
>>>> --
>>>> 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/
>>>>
>>>> _______________________________________________
>>>> Python-ideas mailing list
>>>> Python-ideas at python.org
>>>> https://mail.python.org/mailman/listinfo/python-ideas
>>>> Code of Conduct: http://python.org/psf/codeofconduct/
>>>>
>>>
>>>
>>>
>>> _______________________________________________
>>> Python-ideas mailing list
>>> Python-ideas at python.org
>>> https://mail.python.org/mailman/listinfo/python-ideas
>>> Code of Conduct: http://python.org/psf/codeofconduct/
>>>
>>
>> --
>> 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/
>>
>>
>
--
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