16

In python, is there any way to check if an object is immutable or mutable?

like isimmutable(a) would return True, if a is immutable, else returns False.

rnbguy
  • 1,369
  • 1
  • 10
  • 28
  • 1
    I am not convinced from the answer. Only the answer matches with my need, is `hash()` but it was down voted without any proper explanation. – rnbguy Jul 26 '13 at 07:23
  • Since `immutability` completely depends on the *implementation* of the methods, no there is no way to know whether an object is immutable. – Bakuriu Jul 26 '13 at 07:46
  • Check out their definitions on glossary page : [hashable](http://docs.python.org/2/glossary.html#term-hashable), [immutable](http://docs.python.org/2/glossary.html#term-immutable) – Ashwini Chaudhary Jul 26 '13 at 07:56
  • Since I did not receive proper answer, I started doing some tests on my own and apparently(may be not, I could not find a counter example) this seems to be working. `import copy` `def isimmutable(x): return x is copy.copy(x)`. I would add this as answer, if this is correct ! – rnbguy Jul 26 '13 at 08:41
  • It's not. Plain `object`s are immutable, but copying one produces a new object. Also, if an object is uncopyable, you'll get a `TypeError` instead of an answer. – user2357112 Jul 26 '13 at 08:49
  • 4
    "Since I did not receive proper answer" - you got an answer. The answer was no. There's no way to tell whether an arbitrary object is immutable. – user2357112 Jul 26 '13 at 08:59
  • @user2357112 No I meant, a function which DOES return if a object is immutable or mutable. Anyway which objects are not copyable ? Sorry if I am asking too many question, I am new to python. – rnbguy Jul 26 '13 at 09:00
  • 2
    Iterators aren't copyable, for one example. – user2357112 Jul 26 '13 at 09:04
  • if its a list then it's mutable and if it's a tuple, then it is immutable –  Jul 26 '13 at 12:56

2 Answers2

10

There are no general tests for immutability. An object is immutable only if none of its methods can mutate the underlying data.

take a look at this question

the answer says:

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

that should answer youre question

Community
  • 1
  • 1
Serial
  • 7,925
  • 13
  • 52
  • 71
  • 1
    The selected answer does not specifically answer my question. Though [this](http://stackoverflow.com/a/4374102/1682673) one looks good, but it got downvoted. Can you explain ? – rnbguy Jul 26 '13 at 07:24
  • did you read the answer below it – Serial Jul 26 '13 at 07:31
  • No, I needed a counter example. :P Anyway I asked for it, and someone provided. Anyway I might find a possible answer. Can you check in OP comments ? – rnbguy Jul 26 '13 at 08:44
5

Do you want to check for immutability, or hashability? If you want to check whether something is hashable, hash it:

try:
    hash(thing)
except TypeError:
    print "It's unhashable."
else:
    print "It's hashable."

Hashability is usually what you want. If you want to check whether something is mutable, there's no general test.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • 3
    What are the difference between hashability and mutability ? Because this looks it can be used for immutability [here](http://stackoverflow.com/a/4374102/1682673), though it got downvoted. – rnbguy Jul 26 '13 at 07:26
  • 4
    That answer is wrong. The keys don't have to be immutable, and immutable doesn't imply hashable. Hashable objects can't be mutable *in ways that affect `==` tests*, and immutable objects are not required to provide a `__hash__` method. (Technically, user-defined "immutable" objects are usually initialized mutatively, and you can often mutate something that isn't supposed to be mutable by messing with its private data, but most of the time, hashable objects won't be modified after construction.) – user2357112 Jul 26 '13 at 07:36
  • Thanks a lot. I got the idea now. anyway I found a possible solution, can you check in OP comments ? – rnbguy Jul 26 '13 at 08:45