Freezing

Xavier Morel xavier.morel at masklinn.net
Thu Jan 12 14:53:59 EST 2006


bearophileHUGS at lycos.com wrote:
> The first line of that example has to be:
> 
> s = |set([1, 3, 5])|
> 
> But I don't know/remember why set() can't accept many values like
> max/min:
> 
> max([1,2,5])
> max((1,2,5))
> max(1,2,3)
> 
> Bye,
> bearophile
> 

How about just providing a freeze method on `object` (since everything 
will inherit from object) that can freeze the object?

In fact the freeze protocol could provide 2 methods: freeze and frozen, 
the former would freeze the object in place (e.g. freeze the object it's 
applied to) while the later would return a frozen copy of the object 
it's applied to.

That way, it could even be used as a const-like parameter to a function 
(with frozen)

Examples:
 >>> l = [0, 1, 2, 3]
 >>> l.append(5)
 >>> l
[0, 1, 2, 3, 5]
 >>> l.frozen()
[0, 1, 2, 3, 5]
 >>> fl = l.frozen()
 >>> l.append(7)
 >>> l
[0, 1, 2, 3, 5, 7]
 >>> fl.append(7)
Traceback (most recent call last):
...
WhateverError: frozen 'list' object cannot modified
 >>> fl
[0, 1, 2, 3, 5]
 >>> l.freeze()
 >>> l
[0, 1, 2, 3, 5, 7]
 >>> l.append(9)
Traceback (most recent call last):
...
WhateverError: frozen 'list' object cannot modified

One could even dream of a melt/molten method pair that'd behave at the 
opposite of the freeze/frozen pair (to have the ability to "unfreeze" an 
object if needed)

No new keyword, no new token, no new structure, no new builtin, same 
functionalities.



More information about the Python-list mailing list