python - Recursive function remembers the last result as an argument -


possible duplicate:
“least astonishment” in python: mutable default argument

im trying split number powers of 2, function remembers result of previous call.

from math import log  #split number in powers of 2 #n   :  number #lst :  current list of numbers def spn(n, lst=list()):     if n==1:         lst.append(n)         return lst     else:         #rdy  :  ready-to-add number         rdy=2**((log(n+1,2))-1)         lst.append(rdy)         #n-rdy:  new number evaluate         return spn(n-rdy, lst) 

for example:

spn(1) should return [ 1 ]

spn(3) should return [2.0, 1.0]

spn(7) should return [4.0, 2.0, 1.0]

but works first time call function after first call previous result appears argument.

enter image description here

how can fix that?

change lst argument default value none, , if none instantiate inside function.

read python gotchas on why using mutable default argument values bad.


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -