PyWart: os.path needs immediate attention!

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Jul 29 20:50:02 EDT 2011


Andrew Berg wrote:

>>>>> os.path.exists(path, ignoreSymLnks=False)
> I actually agree with you on these, which I suppose is interesting.


Guido has a rule of thumb: "No constant arguments". Or another way to put
it: if a function takes an argument which is nearly always a constant
(usually, but not always, a flag) then it is usually better off as two
functions.

Especially if the implementation looks like this:

def get_thing(argument, flag):
    if flag:
        return one_thing(argument)
    else:
        return another_thing(argument)


Argument flags which do nothing but change the behaviour of the function
from Mode 1 to Mode 2 are an attractive nuisance: they seem like a good
idea, but aren't. Consider it a strong guideline rather than a law, but
it's one I would think very long and hard about before violating.

But having said that, I'm currently writing a library where nearly all the
functions violate the No Constant Argument rule. (The API isn't yet stable,
so I may still change my mind.) Make of that what you will.


-- 
Steven




More information about the Python-list mailing list