From: rickhg12hs on
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]])

Here are a couple sample runs.

>>> print(recur_trace([],[1,2,3]))
[] [1,2,3]
[1,2,3]

So that worked okay and returned the list [1,2,3].

>>> print(recur_trace([9,8],[1,2,3]))
[9,8] [1,2,3]
[8] [1,2,3,9]
[] [1,2,3,9,8]
None

No list is returned here. Why?
[Using Python 2.6.2]
From: Charles on

"rickhg12hs" <rickhg12hs(a)gmail.com> wrote in message
news:2ff16113-4f79-4dcf-8310-35d2b91e89dc(a)o11g2000yqj.googlegroups.com...
> 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]])
>

shouldn't it be
return recur_trace(x[1:], y + [x[0]])
otherwise the recursive call returns nothing

Charles


From: Cameron Simpson on
On 03May2010 22:02, rickhg12hs <rickhg12hs(a)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(a)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
From: rickhg12hs on
On May 4, 1:34 am, Cameron Simpson <c...(a)zip.com.au> wrote:
> On 03May2010 22:02, rickhg12hs <rickhg1...(a)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.

Ah, an explicit "return" is required. Thanks!
[To bad there's no tail recursion optimization.] 8-(

From: Paul Rudin on
rickhg12hs <rickhg12hs(a)gmail.com> writes:

> 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]])
>
> Here are a couple sample runs.
>
>>>> print(recur_trace([],[1,2,3]))
> [] [1,2,3]
> [1,2,3]
>
> So that worked okay and returned the list [1,2,3].
>
>>>> print(recur_trace([9,8],[1,2,3]))
> [9,8] [1,2,3]
> [8] [1,2,3,9]
> [] [1,2,3,9,8]
> None
>
> No list is returned here. Why?
> [Using Python 2.6.2]

Without trying it out I'd guess you want a "return" in your last
line. (If python falls out of a function without hitting an explicit
return then None is returned by default.)
 |  Next  |  Last
Pages: 1 2
Prev: repeat tkinter
Next: Sphinx hosting