Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for mutability in Python?

Consider this code:

a = {...} # a is an dict with arbitrary contents b = a.copy() 
  1. What role does mutability play in the keys and values of the dicts?
  2. How do I ensure changes to keys or values of one dict are not reflected in the other?
  3. How does this relate to the hashable constraint of the dict keys?
  4. Are there any differences in behaviour between Python 2.x and Python 3.x?

How do I check if a type is mutable in Python?

like image 245
Matt Joiner Avatar asked Dec 07 '10 06:12

Matt Joiner


People also ask

How do you check if a Python is mutable?

An object is mutable if it is not immutable. An object is immutable if it consists, recursively, of only immutable-typed sub-objects. Thus, a tuple of lists is mutable; you cannot replace the elements of the tuple, but you can modify them through the list interface, changing the overall data.

What is mutability in Python?

In technical programming terms, a mutable object is an object whose state can be modified after it is defined. The opposite of a mutable object is an immutable object, whose state cannot be altered after it is initially defined. Examples of immutable objects in Python include integers, floats, strings, and tuples.

What is mutability in Python with example?

Python mutability refers to being able to change an object. Simply put, a mutable object can be changed, but an immutable object cannot. For example, a tuple is an immutable data type. You cannot change its elements after creating it: nums = (1, 2, 3)

How can you tell when an object is mutable?

A mutable object is an object whose state can be modified after it is created. Immutables are the objects whose state cannot be changed once the object is created. Strings and Numbers are Immutable.


1 Answers

1) Keys must not be mutable, unless you have a user-defined class that is hashable but also mutable. That's all that's forced upon you. However, using a hashable, mutable object as a dict key might be a bad idea.

2) By not sharing values between the two dicts. It's OK to share the keys, because they must be immutable. Copying the dictionary, in the copy module sense, is definitely safe. Calling the dict constructor here works, too: b = dict(a). You could also use immutable values.

3) All built-in immutable types are hashable. All built-in mutable types are not hashable. For an object to be hashable, it must have the same hash over its entire lifetime, even if it is mutated.

4) Not that I'm aware of; I'm describing 2.x.

A type is mutable if it is not immutable. A type is immutable if it is a built-in immutable type: str, int, long, bool, float, tuple, and probably a couple others I'm forgetting. User-defined types are always mutable.

An object is mutable if it is not immutable. An object is immutable if it consists, recursively, of only immutable-typed sub-objects. Thus, a tuple of lists is mutable; you cannot replace the elements of the tuple, but you can modify them through the list interface, changing the overall data.

like image 82
Karl Knechtel Avatar answered Sep 27 '22 19:09

Karl Knechtel