Recursive functions not returning lists as expected

Cameron Simpson cs at zip.com.au
Tue May 4 01:34:12 EDT 2010


On 03May2010 22:02, rickhg12hs <rickhg12hs at gmail.com> wrote:
| Would a kind soul explain something basic to a python noob?
| 
| Why doesn't this function always return a list?
| 
| def recur_trace(x,y):
|   print x,y
|   if not x:
|     return y
|   recur_trace(x[1:], y + [x[0]])

You need:
    return recur_trace(x[1:], y + [x[0]])

Otherwise the function returns None.
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

[...] every time you touch something, if your security systems rely
on biometric ID, then you're essentially leaving your pin number on a
post-it note.  - Ben Goldacre, http://www.badscience.net//?p=585



More information about the Python-list mailing list