On Thu, Mar 02, 2017 at 01:08:42PM +0100, M.-A. Lemburg wrote:

> Sorry for the confusion. NoDefault would be usable just like
> any other singleton.

But that is exactly the trouble! We already have a singleton to indicate 
"no default" -- that is spelled None.

Occasionally, we need to allow None as a legitimate value, not just as a 
sentinel indicating a missing value. So the current practice is to 
create your own sentinel. That just pushes the problem back one more 
level: what happens when you have a function where the new NoDefault 
singleton is a legitimate value? You need a *third* sentinel value. And 
a fourth, and so on...


> 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.

Did you miss Serhiy's comment?

    Optional parameters without default value can be 
    positional-or-keyword, keyword-only or positional-only (if 
    the latter is implemented).

It doesn't matter whether the parameter is positional or keyword, or how 
you call the function:

    f(NoDefault)
    f(x=NoDefault)

the effect should be the same.

But I think that's the wrong solution. If it were a good solution, we 
should just say that None is the "No Default" value and prohibit passing 
None as an argument. But of course we can't do that, even if we were 
willing to break backwards compatibility. There are good reasons for 
passing None as a legitimate value, and there will be good reasons for 
passing NoDefault as a legitimate value too.

The problem with having a special value that means "no value" is that it 
actually is a value. Serhiy has a good idea here: cut the gordian knot 
by *avoiding having a value at all*. The parameter name remains unbound. 
If you don't pass a value for the optional parameter, and then try to 
access the parameter, you get a NameError exception! That's really 
clever and I like it a lot.



-- 
Steve
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to