[Python-Dev] PEP 351, the freeze protocol
Nick Coghlan
ncoghlan at gmail.com
Mon Oct 24 11:14:32 CEST 2005
Barry Warsaw wrote:
> I've had this PEP laying around for quite a few months. It was inspired
> by some code we'd written which wanted to be able to get immutable
> versions of arbitrary objects. I've finally finished the PEP, uploaded
> a sample patch (albeit a bit incomplete), and I'm posting it here to see
> if there is any interest.
>
> http://www.python.org/peps/pep-0351.html
I think it's definitely worth considering. It may also reduce the need for "x"
and "frozenx" builtin pairs. We already have "set" and "frozenset", and the
various "bytes" ideas that have been kicked around have generally considered
the need for a "frozenbytes" as well.
If freeze was available, then "freeze(x(*args))" might server as a replacement
for any builtin "frozen" variants.
I think having dicts and sets automatically invoke freeze would be a mistake,
because at least one of the following two cases would behave unexpectedly:
d = {}
l = []
d[l] = "Oops!"
d[l] # Raises KeyError if freeze() isn't also invoked in __getitem__
d = {}
l = []
d[l] = "Oops!"
l.append(1)
d[l] # Raises KeyError regardless
Oh, and the PEP's xdict example is even more broken than the PEP implies,
because two imdicts which compare equal (same contents) may not hash equal
(different id's).
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.blogspot.com
More information about the Python-Dev
mailing list