hashkey/digest for a complex object

MRAB python at mrabarnett.plus.com
Thu Oct 7 11:10:51 EDT 2010


On 07/10/2010 11:42, kj wrote:
> In<mailman.1415.1286438617.29448.python-list at python.org>  Terry Reedy<tjreedy at udel.edu>  writes:
>
>> If these two attributes, and hence the dicts, are public, then your
>> instances are mutable.
>
> I guess I should have written "immutable among consenting adults."
>
> As far as I know, Python does not support private attributes, so
> I guess the dicts are public no matter what I do.  I suppose that
> I can implement "frozendict", but I can't think of any way to
> enforce the immutability of these "frozendicts" that would not be
> trivial to circumvent (it better be, in fact, otherwise I wouldn't
> be able to initialize the damn things).
>
You would initialise them by creating them from a list of tuples (or an
iterable which yields tuples), like with a dict:

 >>> dict([(1, "foo"), (2, "bar")])
{1: 'foo', 2: 'bar'}



More information about the Python-list mailing list