Answer the question
In order to leave comments, you need to log in
Why such strange behavior of the variable after recursion?
Here everything was fine:
def test(mass):
print("До рекурсии:",mass)
if mass==1: return
mass=mass-1
test(mass)
print("после рекурсии:",mass)
test(4)
def test(mass):
print("До рекурсии:",mass)
if len(mass)==1: return
del mass[0]
test(mass)
print("после рекурсии:",mass)
test([4,3,2,1])
Answer the question
In order to leave comments, you need to log in
Because when passing a list as a parameter, it is not the list itself that is copied, but a link to it. When calling recursively, mass
pass mass.copy()
.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question