Before talk about this, let ...
Before talk about this, let's see some definitions.
An object with a fixed value. Immutable objects include numbers, strings and
tuples. Such an object cannot be altered. A new object has to be created if
a different value has to be stored. They play an important role in places where
a constant hash value is needed, for example as a key in a dictionary.
An object is hashable if it has a hash value which never changes during
its lifetime (it needs a hash()method), and can be compared to other objects
(it needs an eq() method). Hashable objects which compare equal must have
the same hash value.
All of Python’s immutable built-in objects are hashable, while no mutable
containers (such as lists or dictionaries) are. Objects which are instances
of user-defined classes are hashable by default; they all compare unequal (except
with themselves), and their hash value is their id().
Objects whose value is unchangeable once they are created are called immutable. (The value of an immutable container object that contains a reference to a mutable object can change when the latter’s value is changed; however the container is still considered immutable, because the collection of objects it contains cannot be changed. So, immutability is not strictly the same as having an unchangeable value, it is more subtle.)
An object’s mutability is determined by its type; for instance, numbers,
strings and tuples are immutable, while dictionaries, sets and lists are mutable.
We can use hash() to check if an object is hashable.
But there seems no method to check if an object is mutable or not.
Google "python mutable check". Seems many people use hash() to check if an
object is mutable or not...
So... the questions are:
Is there any hashable built-in object is mutable?
Is there any un-hashable built-in objects is immutable?
If the answers of the two questions above is "No".
Can we create an user defined type that is hashable but mutable?
Can we create an user defined type that is not hashable but immutable?
If the answers of the four questions above is "No".
Does it mean that we can use hash() to check if an object is mutable or not?
如果覺得這篇文章對你有幫助， 除了留言讓我知道外， 或許也可以考慮請我喝杯咖啡， 不論金額多寡我都會非常感激且能鼓勵我繼續寫出對你有幫助的文章。
If this blog post happens to be helpful to you, besides of leaving a reply, you may consider buy me a cup of coffee to support me. It would help me write more articles helpful to you in the future and I would really appreciate it.