[Tutor] recursive memory (for Q 1)
Lloyd Kvam
pythontutor@venix.com
Mon, 15 Apr 2002 18:54:29 -0400
I have made this same mistake.
def walk(t, f, a, visited={}):
visited is given a clean dictionary only ONCE, when the function is
defined. If you call walk a second time, without specifying visited,
you will simply default to using the same dictionary again.
One solution is:
def walk(t, f, a, visited=None):
if visited is None: visited = {}
This provides a clean dictionary everytime walk is called without the
visited argument.
I can't help on Q2.
Christopher Smith wrote:
> I'm making a modification to the walk() function on the Mac so it avoids
> getting caught in an alias loop between folders (i.e. walking a folder
> which has an alias to another folder which has an alias of the first
> folder). The way I do this is store the file specs in a dictionary. I
> have two questions.
>
> 1)
> Here's what I'm doing:
>
> macpath module
> --------------
> def walk(t, f, a, visited={}):
> .
> .
> .
> for file in listOfFiles:
> .
> .
> .
> if visited.has_key(filespec): continue
> visited[filespec]=''
> t=join(t,file)
> walk(t, f, a, visited) # I pass the modified dictionary
>
> #demo
> walk(t, f, a) #no dictionary sent so it should start at {}
>
> I need the dictionary to start at {} every time I first give the walk, but
> then want it updated as I go deeper in the walk--so the main script call
> leaves the dictionary off but recursive calls send the modified
> dictionary. Is this the right way to go about this?
>
> 2)
> I would also like to find out if an alias points to another (unmounted)
> volume. The only problem is that if the volume is unmounted, the
> ResolveAliasFile routine (or other routines that will give file
> information, like mac.stat()) will prompt me to insert the volume; I would
> like to have the walk be unattended and so would like a way to override
> the insert volume reaction and just know that the alias points to an
> unmounted volume and just skip it. Is there another way to detect this
> condition without being asked to insert the missing volume?
>
> /c
>
>
>
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
--
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358
voice:
603-443-6155
fax:
801-459-9582