Freezing

Raymond Hettinger python at rcn.com
Thu Jan 12 18:33:57 EST 2006


bearophileHUGS at lycos.com wrote:
> add a freeze operation, to freeze lists,
> sets and dicts (etc), so they can be used as keys.

I'm curious whether you've had an actual use for dictionaries as keys.

Likewise, how about frozensets?  Have you had occasion to use them as
keys?  They were created to support sets of sets, yet even that doesn't
come-up often.

There seems to be a disease going around and those infected become
irresistibly fascinated with freezing.   After developing a resistance
to practical applications, the infection becomes feverish resulting in
delirious proposals to change the entire language to accommodate the
freezing (these have included eliminating tuples, making strings
mutable, recursive freezing, and adding a __freeze__ protocol to all
containers).

For some reason, it sounds charming to be able to use a dictionary as a
key, yet it loses its grace when phrased in terms of what can already
be done:

  k = frozenset(mydict.iteritems())
  somedict[k] = someval

AFAICT, no one seems to write code like this.  So why build a mechanism
to automate a process that no one uses?

Also note that Guido has said over and over that tuples are NOT
frozenlists.  Even with a mechanism to freeze lists, tuples won't go
away.

One other nit.  The term "freezing" inaccurately suggests an in-place
operation; however, the PythonWay(tm) is to create new objects (i.e.
given a mutable set s, the result of frozenset(s) is a new container).
 The non-PythonWay is to have state flag indicating frozenness and have
that flag disable mutating methods while enabling a __hash__ method.


Raymond




More information about the Python-list mailing list