Making immutable instances

bonono at gmail.com bonono at gmail.com
Fri Nov 25 04:32:31 EST 2005


Paul Rubin wrote:
> Fair enough.  How's this:
>
>    a = ImmutableObject()
>    b = deepcopy(a)
>    assert a == b  # a and b start out equal
>    .... do stuff ....
>    # since a and b are immutable, they should still be equal
>    # no matter what has happened above
>    assert a == b
>
> If you've added attributes to a but not to b, they should compare
> unequal, breaking immutability.

How about this ?

a=(1,[])
b=copy.deepcopy(a)
assert a==b # True
a[1].append(1)
assert a!=b




More information about the Python-list mailing list