Why do you think tuple is an immutable in Python?

PythonServer Side ProgrammingProgramming

Tuples are immutable because of the following reasons −

  • maintaining order − Tuples are mainly defined in python as a way to show order. For example, when you retrieve data from a database in form of list of tuples, all the tuples are in the order of the fields you fetched.

  • copy efficiency − rather than copying an immutable object, you can alias it (bind a variable to a reference)

  • comparison efficiency − when you're using copy-by-reference, you can compare two variables by comparing location, rather than content

  • interning − you need to store at most one copy of any immutable value there's no need to synchronize access to immutable objects in concurrent code

  • const correctness − some values shouldn't be allowed to change.

Published on 05-Jan-2018 17:13:01