[issue30413] Add fnmatch.filterfalse function

Steven D'Aprano report at bugs.python.org
Wed Aug 21 07:10:49 EDT 2019


Steven D'Aprano <steve+python at pearwood.info> added the comment:

On Wed, Aug 21, 2019 at 10:51:02AM +0000, STINNER Victor wrote:

> Rather than adding a new function, why not adding a parameter like 
> sort(key=func, reverse=True)? Something like fnmatch.filterfalse(pat, 
> invert=True)?

Guido argues that as a general rule of thumb, we should avoid "constant 
bool parameters" and prefer seperate functions. For example, we have

- re.search and re.match, not re.search(start=True)

- str.find and str.rfind, not str.find(end=False)

If we typically call the function with a bool constant:

    filter(pat, invert=True)

rather than a variable or expression

    filter(pat, invert=condition or default)

that's a hint that the two cases probably should be seperate functions.

Martin Fowler agrees:

https://martinfowler.com/bliki/FlagArgument.html

as does Raymond Chen:

https://devblogs.microsoft.com/oldnewthing/20060828-18/?p=29953

(Of course there are cases where it is impractical to avoid bool flags 
-- if a function would otherwise take four flags, we would need sixteen 
functions! -- or there may be other reasons why we might go against that 
design rule.)

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue30413>
_______________________________________


More information about the Python-bugs-list mailing list