how to test for atomicity/mutability/hashability?

Arnaud Delobelle arnodel at gmail.com
Thu Oct 7 23:22:45 CEST 2010


Chris Rebert <clp2 at rebertia.com> writes:

> On Thu, Oct 7, 2010 at 1:46 PM, Arnaud Delobelle <arnodel at gmail.com> wrote:
[...]
>> I think defining mutability is subject to opinion, but here is a first
>> approximation.
>>
>>
>> def mutable(obj):
>>    return obj.__hash__ is None or type(obj).__hash__ == object.__hash__
>
> Corner case (I think):
>>>> a = object()
>>>> a.b = "c"
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> AttributeError: 'object' object has no attribute 'b'
>>>> mutable(a)
> True

Ah true.  There was also the problem that e.g. mutable(tuple) was False.

How about this second approximation:

def mutable(obj):
    h = type(obj).__hash__
    return not h or h is object.__hash__ and type(obj) is not object

-- 
Arnaud



More information about the Python-list mailing list