[Python-Dev] AC Derby and accepting None for optional positional arguments

Guido van Rossum guido at python.org
Thu Jan 16 19:57:39 CET 2014


On Thu, Jan 16, 2014 at 12:31 AM, Serhiy Storchaka <storchaka at gmail.com> wrote:
> getattr(foo, 'bar', None) is not the same as getattr(foo, 'bar'). So None
> can't be used as universal default value.

Not universal, but I still think that most functions don't need to
have such a subtle distinction.

E.g. in the case of sha1() I still believe that it's totally fine to
switch the default to b''. In that particular case I don't see the
need to also accept None as a way to specify the default.

Basically, my philosophy about this is that anytime you can't easily
reimplement the same signature in Python (without reverting to
manually parsing the args using *args and **kwds) it is a pain, and
you should think twice before canonizing such a signature.

Also, there are two somewhat different cases:

(a) The default can easily be expressed as a value of the same type
that the argument normally has. This is the sha1() case. In this case
I see no need to also accept None as an argument (unless it is
currently accepted, which it isn't for sha1()). Another example is
<stream>.read() -- here, passing in a negative integer means the same
as not passing an argument.

(b) The default has a special meaning that does something different
than any valid value. A good example is getattr(), which must forever
be special.

To me, most functions should fall in (a) even if there is currently
ambiguity, and it feels like repeat() was *meant* to be in (a).

I'm not sure how AC should deal with (b), but I still hope that true
examples are rare enough that we can keep hand-coding them.

-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-Dev mailing list