You are not logged in.
Edit: Durrr. Apparently I had a senior moment. The solution, of course, is that line 7 of the function must be return splitstring(c, list)
Before I realized Python strings have a split() method, I made the following function:
def splitstring(string, list=None):
if list is None:
list = []
a, b, c = string.partition(' ')
list.append(a)
if c:
splitstring(c, list)
else:
return list
But it returns None unless no splitting is done, and I don't understand why.
>>> if splitstring('d s t n') is None:
... print('WTF!?!')
...
WTF!?!
If I print(list) instead of return list, the expected list is printed. So list isn't actually None...
Last edited by alphaniner (2014-01-17 22:02:47)
But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner
Offline
My guess is that your there is a codepath in your function that doesn't return anything, which is python is None.
if c:
splitstring(c, list)
# nothing returned
else:
return list
Offline
Remove the else, it must return the list after the recursion ends.
if c:
splitstring(c, list)
return list
Offline
This really was just a brainfart. I've made functions like this before, always in the form
if foo:
return foobar
else:
return bar
I've even explained to others why that works and what I originally posted fails. Oy.
But I had never realized it could be done in the way you suggest. Thanks!
But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner
Offline