Freezing a mutable (was Re: lambda)

Bengt Richter bokr at oz.net
Thu Jan 20 19:20:06 EST 2005


On 20 Jan 2005 14:07:57 GMT, Antoon Pardon <apardon at forel.vub.ac.be> wrote:

>Op 2005-01-20, Nick Coghlan schreef <ncoghlan at iinet.net.au>:
>> 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).
>
>The problem here is IMO is the, we are all consenting adults (which we
>are not but I wont start on this now), approach.
>
>I have been thinking a bit in your freeze direction, but more thorough.
>The idea i had was that freeze would replace all mutating methods
>with methods that would throw an exception. a thaw method would inverse
>the proces. It would then be the responsibilty of the dictionary to
>freeze an object on entry and thaw it when removed. However this
>wouldn't work with ordinary python objects, since there is no easy way
>to have readonly or private variables.
>
Would you like a dictionary that acts as you want and takes care of all
problems internally, and accepts keys and values of any type without wrapping
or other modification -- or do you want a wrapper that can make any object
suitable for use as key or value in python's curent definition of dict?

Just DYFR please. You still haven't you know ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list