3rd argument to os.path.walk(): Why?
Alex Martelli
aleaxit at yahoo.com
Mon Dec 11 09:15:56 EST 2000
More information about the Python-list mailing list
Mon Dec 11 09:15:56 EST 2000
- Previous message (by thread): 3rd argument to os.path.walk(): Why?
- Next message (by thread): 3rd argument to os.path.walk(): Why?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
"James T. Dennis" <jadestar at idiom.com> wrote in message news:912gdb$1oqr$1 at news.idiom.com... > In the _Python_Essential_Reference_, by David Beazley > (New Riders Publishing, 2000) I notice that os.path.walk() > takes three arguments which he lists as: > > path, visitfunc, arg So it does. > It notes that the function referenced in visitfunc is called > with (arg, dirname, namelist) for every directory that is traversed > by the os.path.walk. And, indeed, that's what happens. > I don't understand what the "args" is for. But you do, as you say later: > ignoring the 'args' argument in my functions. I guess I > could write my functions to behave differently based on > the contents of the args. (I can even imagine that as > useful, though I don't need it for anything right now). Exactly. Could come in useful at some time (make the function reentrant by avoiding having it refer to a global variable, etc). E.g., say you want to _count_ the files: import os class Counter: def __init__(self): self.count = 0 def inc(self, n=1): self.count += n def count_names(count, dir, flist): count.inc(len(flist)) count = Counter() os.path.walk("/", count_names, count) print count.count,"files in all" You could do it without the argument by having the function access count as a global variable, but if global-variables can be easily avoided, so much the better, don't you think? > Why is this third argument required in os.path.walk()? Why required as opposed to optional? Dunno, it seems that it might well have been made optional, defaulting, say, to None. But this seems to be a very minor issue -- it's not hard to pass in the third-argument as None in an explicit way rather than letting it default, for example. Alex
- Previous message (by thread): 3rd argument to os.path.walk(): Why?
- Next message (by thread): 3rd argument to os.path.walk(): Why?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list