Making immutable instances

Mike Meyer mwm at mired.org
Fri Nov 25 03:42:06 CET 2005


Paul Rubin <http://phr.cx@NOSPAM.invalid> writes:
> "Giovanni Bajo" <noway at sorry.com> writes:
>> > However, when you prevent a client from adding an attribute, you're
>> > not merely making your objects immutable, you're making them
>> > static.
> No I don't believe that.  If an object is immutable, then
> obj.serialize() should return the same string every time.  If you can
> add attributes then the serialization output will become different.

There isn't a standard serialize method in Python, so I don't know how
you want to define it. 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.

Personally, I'd argue that serialize shouldn't return the extra
attribute. If you want to add an attribute that gets serialized, you
need to subclass the immutable class, add the attribute there, and
extend serialize to include it. Whether or not that's reasonable
depends on how you want to define serialize.

       <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list