Global variable messes up my recursive function <p>I've just run into a tricky issue. The following code is supposed to split words into chunks of length <code>numOfChar</code>. The function calls itself, which makes it impossible to have the resulting list (<code>res</code>) inside the function. But if I keep it outside as a global variable, then every subsequent call of the function with different input values leads to a wrong result because <code>res</code> doesn't get cleared.</p> <p>Can anyone help me out?</p> <p>Here's the code (in case you are interested, this is <a href="http://www.pyschools.com/quiz/view_question/s7-q23" rel="nofollow">problem 7-23 from PySchools.com</a>):</p> <pre><code>res = [] def splitWord(word, numOfChar): if len(word) &gt; 0: res.append(word[:numOfChar]) splitWord(word[numOfChar:], numOfChar) return res print splitWord('google', 2) print splitWord('google', 3) print splitWord('apple', 1) print splitWord('apple', 4) </code></pre>