Xavier Morel <[EMAIL PROTECTED]> writes: >[EMAIL PROTECTED] wrote: >> Dicts and sets require immutable keys, like tuples or frozensets, but >> to me they look like a duplication. So the idea is to remove tuples and >> frozensets (and replace the few other uses of tuples with lists, like >> the % interpolation), and to add a freeze operation, to freeze lists, >> sets and dicts (etc), so they can be used as keys.
This has been suggested before. > How about just providing a freeze method on `object` (since everything > will inherit from object) that can freeze the object? Well, the OP suggested this is for 3.0, but it doesn't have to be unless you use his syntax. But Python generally prefers words to magic characters, so a "freeze" builtin would be preferable, meaning it could be done in 2.x. > 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. Freezing in place is problematical. For this to work as intended, all the values in the container have to be frozen as well. All yours and the OPs examples used immutable values, so this wasn't obvious. But consider: d = dict() l = [d] l.freeze() for l to be usable as a dictionary key etc. after the freeze invocation, d must also be frozen. Doing that in place would be unfortunate. Creating a frozen copy of d to put in the frozen version of l might work, but still seems strange. Furthermore: > 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) You can't do unmelt in place: l.freeze() d.melt() would leave l not really frozen. You probably want the freeze and melt to be symmetric, which lets out freezing in place. > No new keyword, no new token, no new structure, no new builtin, same > functionalities. Actually, I like the "len" model, which would be a new builtin that uses the __freeze__ method. <mike -- Mike Meyer <[EMAIL PROTECTED]> http://www.mired.org/home/mwm/ Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information. -- http://mail.python.org/mailman/listinfo/python-list