Making immutable instances

Paul Rubin http
Fri Nov 25 10:23:03 CET 2005


Mike Meyer <mwm at mired.org> writes:
> There isn't a standard serialize method in Python, so I don't know how
> you want to define it.

Well, consider pickle, for example.

> I can think of perfectly reasonable definitions
> of serialize where obj.serialize() won't always return the same string
> on an immutable object, even if you don't allow adding attributes.

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.



More information about the Python-list mailing list