why do functions save variable definitions when using python from the command line?

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.

Get our expert's

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this and thousands of other questions.

A community for students.

why do functions save variable definitions when using python from the command line?

MIT 6.00 Intro Computer Science (OCW)
See more answers at brainly.com
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.

Get this expert

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this and thousands of other questions

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?
Example?
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.

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

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

Not the answer you are looking for?

Search for more explanations.

Ask your own question