1

What is the difference in the memory management of Mutable and Immutable Data Structures in Python Programming Language?

Abhijit Sarkar
25.3k23 gold badges136 silver badges251 bronze badges
asked Nov 11, 2021 at 0:14
8
  • 1
    In most cases, no difference. Commented Nov 11, 2021 at 0:17
  • There are none, other than some CPython optimizations that are entirely implementation details that shouldn't be relied on (e.g. the small integer cache, the interning of certain strings, etc) Commented Nov 11, 2021 at 0:18
  • Wait, are e you asking about Scala or Python? Those are two pretty different languages Commented Nov 11, 2021 at 0:18
  • Asking about Python Mutable and Immutable data strucutres Commented Nov 11, 2021 at 0:40
  • 1
    @AbhijitSarkar tuple, all the numeric types (int, float, complex, fractions.Fraction, decimal.Decimal), str, bytes, frozenset.... maybe more Commented Nov 11, 2021 at 10:29

2 Answers 2

2

The Python Language Specification does not say anything about Memory Management at all, so it obviously also doesn't say anything about Memory Management of Mutable Data Structures nor does it say anything about Memory Management of Immutable Data Structures.

Every Python Implementation is free to manage their memory however they wish. For example, some use Reference Counting, some use a Tracing Garbage Collector, some use both, some don't have their own Memory Manager at all and rely on the underlying host platform.

There is nothing in the Python Language Specification that would force implementors to treat Mutable and Immutable Data Structures the same, there is also nothing in the Python Language Specification that would force implementors to treat Mutable and Immutable Data Structures differently.

answered Nov 11, 2021 at 6:21
Sign up to request clarification or add additional context in comments.

Comments

-1

Mutable is a fancy way of saying that the internal state of the object is changed/mutated.

So, the simplest definition is: An object whose internal state can be changed is mutable. On the other hand, immutable doesn’t allow any change in the object once it has been created.

You can find out more here.

There isn't really much difference, just the way the data is compiled. If your on an older machine/OS, it might take longer to compile your data structure or use a different algorithm to save time.

answered Nov 12, 2021 at 0:38

1 Comment

This has nothing to do with compilation.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.