What behavior would you expect?

Chris Angelico rosuav at gmail.com
Thu Feb 19 10:08:49 EST 2015


On Fri, Feb 20, 2015 at 1:16 AM, Denis McMahon <denismfmcmahon at gmail.com> wrote:
>> 2. no files match the given pattern
>
> Return either None, 0, False or an empty string.
>
> In both cases, it is then a matter for the calling code to catch the
> exception or handle the return value appropriately.

I'd avoid the empty string here, because "absence of file" should be
very different from "first file matching pattern". Imagine this naive
code:

fn = my_function("/path/to/dir", "pattern2")
move_to("/path/to/dir/" + fn, "/other/path")

In the failure case (nothing matches the pattern), what could happen?
Best is it raises an exception, which would come from the my_function
line. Next best, it returns None, 0, or False, and you get a TypeError
on the deletion, and can trace your way back up. Worst, it returns ""
and oops, you just moved the whole directory into the target, instead
of just one file.

Of course, non-naive code probably prefers a None/0/False return to a
raised exception, but in any case, the difference between those two
options is fairly small. But I'd avoid the potential confusion with
the empty string.

Anyway, that's just API bike-shedding :)

ChrisA



More information about the Python-list mailing list