Message350066
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.) |
|
Date |
User |
Action |
Args |
2019-08-21 11:10:49 | steven.daprano | set | recipients:
+ steven.daprano, brett.cannon, vstinner, serhiy.storchaka, wolma, Roee Nizan |
2019-08-21 11:10:49 | steven.daprano | link | issue30413 messages |
2019-08-21 11:10:49 | steven.daprano | create | |
|