On the variability of tuples and dictionaries in python.

for doubts about variable objects, please give us some advice. Thank you!

problem description:

    The element of
  1. tuple tuple can only hold immutable objects, and the element in it can be list list , because what is actually stored is the address of the list (variable object). As long as the address of the variable list remains the same, even if the elements in the list change, it cannot be said to be a change of tuple elements.
  2. The key of the
  3. dictionary dict cannot be a mutable element because you need to use key to hash the value. If key is a list (mutable object) at this time, the dictionary will report an error: TypeError: unhashable type: "list" .

then the problem arises:
is also a variable element list . Why is it legal and immutable to take the address in the tuple tuple , but the value as key in the dictionary dict is illegal, takes value , and is variable.

such a design does not feel consistent in use or intuitive understanding, because the rules are inconsistent.

Dec.02,2021

The key of

dict must be able to hash
tuple is a sequence of values. Where the value can be any type , using an integer index,


"" <br>""

<blockquote>objects whose value is unchangeable once they are created are called immutable.<br>value"" </blockquote>

value

<blockquote>Some objects contain references to other objects; these are called containers. Examples of containers are tuples, lists and dictionaries. The references are part of a container's value.<br>/value"id" idvaluepythonData model</blockquote>
>>> foo=([1,2], [3,4]) -sharp
>>> [id(i) for i in foo] -sharpidvalue
[1747679411912, 1747679411848]
>>> foo[0][0]=5 -sharpvaluevalue
>>> foo
([5, 2], [3, 4])
>>> [id(i) for i in foo] -sharpvalue
[1747679411912, 1747679411848]
Menu