0

I'm having some issues. I'm learning about dynamic pgrogramming and want to create a function getContWordPermutation(str) that finds all continues substring permutations. for example:

>>>getContWordPermutation('abc')
['abc', 'bc', 'c', 'b', 'ac', 'a', 'ab']

my code runs perfectly once.

def getContWordPermutation(word,m=[]):
if word == '':
    return
if word not in m:
    m.append(word)
for i in range(0,len(word)):
    newWord = word[0:i]+word[i+1:]
    if word not in m:
        m.append(word[:-i])
    getContWordPermutation(newWord)
return m

this runs perfectly as according to my example. But somehow this list m lives globally, because next call of function is being affacted of first call

>>>getContWordPermutation('abc')
>>>getContWordPermutation('de')
['abc', 'bc', 'c', 'b', 'ac', 'a', 'ab', 'de', 'e', 'd']

please help me understand, i'm going nuts

nammerkage
  • 232
  • 2
  • 17
  • 2
    Default arguments are created at function definition time not call time. The default is an attribute of the function itself so there is only ever one instance. Note that it is not global. – Holloway Jan 20 '20 at 13:52
  • thank you @Holloway – nammerkage Jan 20 '20 at 14:13

0 Answers0