os.walk walks too much

Jeff Epler jepler at unpythonic.net
Fri Feb 27 22:04:49 CET 2004


On Fri, Feb 27, 2004 at 02:51:04PM -0500, Marcello Pietrobon wrote:
> dirs[:] creates a copy of dirs

This creates a new list which contains the same items as the list named
by dirs

> dirs[:] = [] - empty the original list

This changes the items in the list named by dirs.  It replaces (mutates)
the range named on the left-hand of = with the items on the right-hand.

> and
> dirs = [] - empty a copy of the original list

This makes dirs name a different list than it did before, but the value
of the list that dirs named a moment ago is unchanged.

In the case of os.walk (or anywhere you do something by mutating an item
passed in) you have to change the items in a particular list ("mutate
the list") , not change the list a particular local name refers to.

Jeff




More information about the Python-list mailing list