[Tutor] Recursion help

Justin Heath justin@unixremedies.com
Thu Jul 31 18:07:31 2003


I am working thru some of the examples in the "Learning to Program" 
tutorial. I am going thru the recusrion section and have gotten stumped. 
Here is the code I have run below:

def printList(L):
    # if its empty do nothing
    if not L: return
    # if its a list call printList on 1st element
    if type(L[0]) == type([]):
        printList(L[0])
    else: #no list so just print 
        print L[0] 
    # now process the rest of L 
    printList(L[1:])

myList=[1,2,3,4,5]

printList(myList)

The output is as follows:
1
2
3
4
5

It "appears" that the code is running thru the else block over and over 
until it reaches the end of the list. However, I do not see how the list 
is being enumerated or how it knows to go to the next item in the list. 
Can anyone shed some light on this?

Thanks,
Justin