[Python-ideas] Boolean parameters guidelines

Guido van Rossum guido at python.org
Mon May 9 15:47:08 EDT 2016


On Mon, May 9, 2016 at 12:36 PM, Barry Warsaw <barry at python.org> wrote:

> On May 08, 2016, at 11:59 AM, Steven D'Aprano wrote:
>
> >I think it is preferable *not* to have boolean parameters at all.
> >
> >I don't remember if this is Guido's name for it, but I remember him
> >expressing the guideline "No constant bool arguments". If you have an
> >argument which takes a bool, and is used solely to switch between two
> >different modes, and the caller will most likely call the function with
> >the argument as a constant known when writing the code (rather than
> >taking an expression or variable with value not known until runtime),
> >then it is usually better to split the function into two, one for
> >each mode.
>
> I think Guido's point is stricter than that (but I don't want to put words
> in
> his mouth :).  It's that a boolean flag argument shouldn't change the
> return
> type of the method.  Flags can certainly change the behavior of a method
> and I
> think that's perfect fine.
>

These are two different things. Any time the *value* of an argument affects
the *type* of the return value there's a problem, not just for boolean
parameters. E.g. open(..., "rb") vs. open(..., "r") is one of the worst
offenders -- and "r" vs. "w" is another one for open()!

The thing Steven quotes is specific to boolean parameters and pretty much
exactly what I would say about them.

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160509/3ddb3ef2/attachment.html>


More information about the Python-ideas mailing list