Search code examples
pythondefault-arguments

Specify None as default value for Boolean function argument?


The prototype for numpy.histogram contains an input argument density of type bool and default value None. If the caller does not supply density, what value does it take on?

The closest Q&A that I can find is this. The first answer says "Don't use False as a value for a non-bool field", which doesn't apply here. It also says that bool(x) returns False, but that doesn't assure the caller that the function will set density to False if it isn't provided. Is this a mistake in the documentation of the prototype for numpy.histogram, or am I missing something about the documentation convention?

The other answers to the above Q&A do not seem relevant to my question.


Solution

  • This is at best poorly documented. The documentation seems to imply that False and the default None will be treated equivalently, but it should either document that explicitly, or use the more sensible False as the default value. (The third option would be if the function makes a distinction between False and None, but that also should be explicitly documented.)