Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

According to this Python code visualizer, Python strings are allocated on the stack and not on the heap.

Why is this? I thought they would be similar to Java where Strings are allocated on the heap.

share|improve this question
add comment

2 Answers 2

up vote 7 down vote accepted

That visualizer is not showing the string data on the stack. It is showing the local references to the heap data as part of the call stack. This is very similar to Java where String references are local variables that point to actual String objects on the heap.

The visualizer is free to make any kind of representation simplification it cares to. It does not imply that "Python strings are allocated on the stack" in any given implementation of Python.

share|improve this answer
add comment

Has Greg said in his answer, strings are allocated on the heap.

The visualizer has choosen to display some objects in the "frame" part, which seems wrong to me. Fortunately, it has little impact for someone learning python, since affected objects (int, string) are immutable. So reasoning about effects is still possible.

However it gives the false impression that strings are duplicated in memory when you do things like:

x = "hello"
y = x
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.