[Python-ideas] Allow iterable argument to os.walk()

Guido van Rossum guido at python.org
Mon Oct 31 02:44:40 CET 2011


On Sun, Oct 30, 2011 at 1:51 PM, John O'Connor <jxo6948 at rit.edu> wrote:
>> No, because it means you end up having to special case strings so
>> they're treated atomically. We already do that in a few
>> string-specific APIs (e.g. startswith()/endswith()), but it's still
>> not a particularly nice pattern.
>
> It would be a special case, but I don't understand why that is a bad
> thing or why it is appropriate to do in some places but not others.

Maybe you forgot what the Zen of Python says about special cases. :-)

"Special cases aren't special enough to break the rules."

> Perhaps I am missing something. In this case, I think conceptually it
> makes sense to perform the "walk" on an iterable and also that one
> would expect strings to be treated atomically. I am well aware that
> not every 2-3 line recipe needs to be a new function but having more
> routines operate on iterables (where it makes sense) seems like a good
> idea.

It is often a good idea when the routine currently takes a specific
iterable type (e.g. list). It is rarely a good idea to have a function
that acts either on something of type X or a list of things of type X.

> As an aside, I question the aesthetics of yet another for loop for the
> following. At least python has a nice for loop construct :)
>
> for dir in dirs:
>    for root, dirs, files in os.walk(dir):
>        for name in files:
>            ....
>        for name in dirs:
>            ....

Whatever. :)

-- 
--Guido van Rossum (python.org/~guido)



More information about the Python-ideas mailing list