recursion problem

Uwe Schmitt schmitt at num.uni-sb.de
Mon Sep 17 16:39:16 CEST 2001


Hi, i wrote the Python-script below in order to find
a path in a graph. It is recursive.

   lst="                                                           "

   def path(g, p0, pgoal, depth=0):

       global v
   
       if (p0==pgoal): return [p0]
   
       visited[p0]=1    
       for pi in neighbour(g,p0):        # returns list of neighbours
          if not visited.has_key(pi):
             print lst[:3*depth], ">>>", pi, pgoal
             retval = path(g, pi, pgoal, depth+1)
             print lst[:3*depth], "<<<", retval 
	     return retval.append(p0)
   
       return []
   
   # edges in the graph:
   # there is a edge between node 1 and node 2, etc....

   g=[(1,2),(2,4),(1,3),(1,4),(3,4),(3,5),(5,6),(4,6)]


   
   visited={}; print weg(g,2, 5)


i get the following output:

 >>> 1 5
    >>> 3 5
       >>> 4 5
          >>> 6 5
             >>> 5 5
             <<< [5]
          <<< None

and then i get a message like "None object has no attribute append()"...


Where does the "None" in the last line come from ? I don't understand
this behaviour of my routine. Each return-statement in my
script returns a list...

Is this a python bug ? Is it possible that the stack is corrupted ???
Or am i just stupid ?

Yours, Uwe.

    

-- 
Uwe.Schmitt at num.uni-sb.de      Universität des Saarlandes
phone: +49 (0)681/302-2468     Geb. 36.1, Zi. 4.17, PF 151150
                               D-66041  Saarbrücken
http://www.rocksport.de        http://www.rocksport.de/first_ride.mp3




More information about the Python-list mailing list