Dreaming of new generation IDE

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Feb 6 00:29:10 CET 2010

On Fri, 05 Feb 2010 22:22:39 +0000, bartc wrote:

> "Steve Holden" <steve at holdenweb.com> wrote in message
> news:mailman.1998.1265399766.28905.python-list at python.org...
>> Arnaud Delobelle wrote:
>>> Robert Kern <robert.kern at gmail.com> writes:
>>>> I prefer Guido's formulation (which, naturally, I can't find a direct
>>>> quote for right now): if you expect that a boolean argument is only
>>>> going to take *literal* True or False, then it should be split into
>>>> two functions.
>>> So rather than three boolean arguments, would you have eight
>>> functions?
>> If there's genuinely a need for that functionality, yes.
> So you want a function such as drawtext(s, bold=true, italic=false,
> underline=true) to be split into:
> drawtext(s)
> drawtextb(s)
> drawtexti(s)
> drawtextu(s)
> drawtextbi(s)
> drawtextbu(s)
> drawtextiu(s)
> drawtextbiu(s)

No, of course not. But one might prefer a function with an alternate API, 
such as:

style = TextStyle(bold=True, underline=True, size=12, font='helvetica')
drawtext(s, style)
style.italic = True
drawtext(s, style)

Other alternatives might be to pass the style information as a string 
"BU" (bold underline) or a numeric flag (BOLD & UNDERLINE).

In general, if your implementation looks like this:

def function(args, flag):
    if flag:

then this is a good candidate for splitting into multiple functions.


def function(args, flag):
    result = do_this()
    if flag:
        result = modify(result)
    return result

or similar.

The point is that such a function uses the flag to select between two 
different semantics. From a API perspective, it is generally better to 
make each behaviour an independent function (perhaps calling a third, 
private, function implementing any common behaviour). For example, 
instead of:

def extreme(sequence, biggest=True):
    """Return the extreme value from sequence.

    If biggest is a true value, return the maximum value, otherwise
    return the smallest.
    pass  # implementation left as an exercise

have two functions, max and min.


More information about the Python-list mailing list