Freezing a mutable (was Re: lambda)

Nick Coghlan ncoghlan at iinet.net.au
Thu Jan 20 07:35:18 EST 2005


Antoon Pardon wrote:
> I missed that you would use it with the idiom: dct[x.frozen()]

The list itself isn't hashable with this approach, so you don't have much 
choice. I wasn't particularly clear about that point, though.

> I have two problems with this approach.
> 
> 1) It doesn't work when you get your keys via the keys/items
>    methods.

True - the frozen object has no link back to the original object. That could be 
added though (by returning a tuple subtype with the extra attribute)

> 2) This is rather minor, but a user could still unfreeze
>    untimely 

True - doing that is less likely than mutating a hashable list though :)

I'm just noting this as a way to avoid copying data more than once when storing 
immutable copies of mutable data in a dictionary. You're quite right that there 
isn't a really clean idiom for doing that in Python (aside from moving to a 
different data structure that works natively as a dict key, naturally).

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net



More information about the Python-list mailing list