anonymous
  • anonymous
why do functions save variable definitions when using python from the command line?
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
I'm using the python environment in a Linux terminal, and I keep getting errors when I try to use functions twice, because somehow the function retains the definitions of variables from previous calls. This doesn't happen when I run the same functions in IDLE on my Mac. Any ideas?
anonymous
  • anonymous
Example?
anonymous
  • anonymous
def test (inputString, testList = []): for x in inputString: testList += x, return inputString[len(testList) - 1] Actually, it's happening on my Mac too. I think there's something I don't understand about default variables. the function above tests the problem I'm having. It's supposed to return the last character in the string by assigning testList to the empty list by default (the function I was working on when I found this problem included a default variable, so I wanted to include one), then to add each character in the inputString to the list, measure the length of the list, and try to return the last position from the string. What happens is that if I run it once, using test('fgzsjtztj') it works. If I run it again with the same input, it gives me an indexerror saying that the inputString string index is out of range. I can see that the letters from the previous inputString are still in the default for testList.

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
I believe this link addresses your question: http://boodebr.org/main/python/tourist/mutable-obj-as-default-arg
anonymous
  • anonymous
That is it exactly. Thanks!

Looking for something else?

Not the answer you are looking for? Search for more explanations.