mwm at mired.org
Thu Jan 12 18:52:55 EST 2006
Xavier Morel <xavier.morel at masklinn.net> writes:
>bearophileHUGS at lycos.com 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
d = dict()
l = [d]
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.
> 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:
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
Actually, I like the "len" model, which would be a new builtin that
uses the __freeze__ method.
Mike Meyer <mwm at mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
More information about the Python-list