Steven D'Aprano wrote:
> M.-A. Lemburg wrote:
>> Victor Stinner wrote:
>>>> See also the PEP 351.
>>> I read the PEP and the email explaining why it was rejected.
>>>
>>> Just to be clear: the PEP 351 tries to freeze an object, try to
>>> convert a mutable or immutable object to an immutable object. Whereas
>>> my frozendict proposition doesn't convert anything: it just raises a
>>> TypeError if you use a mutable key or value.
>>>
>>> For example, frozendict({'list': ['a', 'b', 'c']}) doesn't create
>>> frozendict({'list': ('a', 'b', 'c')}) but raises a TypeError.
>>
>> I fail to see the use case you're trying to address with this
>> kind of frozendict().
>>
>> The purpose of frozenset() is to be able to use a set as dictionary
>> key (and to some extent allow for optimizations and safe
>> iteration). Your implementation can be used as dictionary key as well,
>> but why would you want to do that in the first place ?
> 
> Because you have a mapping, and want to use a dict for speedy, convenient 
> lookups. Sometimes your
> mapping involves the key being a string, or an int, or a tuple, or a set, and 
> Python makes it easy
> to use that in a dict. Sometimes the key is itself a mapping, and Python 
> makes it very difficult.
> 
> Just google on "python frozendict" or "python immutabledict" and you will 
> find that this keeps
> coming up time and time again, e.g.:
> 
> http://www.cs.toronto.edu/~tijmen/programming/immutableDictionaries.html
> http://code.activestate.com/recipes/498072-implementing-an-immutable-dictionary/
> http://code.activestate.com/recipes/414283-frozen-dictionaries/
> http://bob.pythonmac.org/archives/2005/03/04/frozendict/
> http://python.6.n6.nabble.com/frozendict-td4377791.html
> http://www.velocityreviews.com/forums/t648910-does-python3-offer-a-frozendict.html
> http://stackoverflow.com/questions/2703599/what-would-be-a-frozen-dict

Only the first of those links appears to actually discuss reasons for
adding a frozendict, but it fails to provide real world use cases and
only gives theoretical reasons for why this would be nice to have.

>From a practical view, a frozendict would allow thread-safe iteration
over a dict and enable more optimizations (e.g. using an optimized
lookup function, optimized hash parameters, etc.) to make lookup
in static tables more efficient.

OTOH, using a frozendict as key in some other dictionary is, well,
not a very realistic use case - programmers should think twice before
using such a design :-)

>> If you're thinking about disallowing changes to the dictionary
>> structure, e.g. in order to safely iterate over its keys or items,
>> "freezing" the keys is enough.
>>
>> Requiring the value objects not to change is too much of a restriction
>> to make the type useful in practice, IMHO.
> 
> It's no more of a limitation than the limitation that strings can't change.
> 
> Frozendicts must freeze the value as well as the key. Consider the toy 
> example, mapping food
> combinations to calories:
> 
> 
> d = { {appetizer => fried fish, main => double burger, drink => cola}: 5000,
>       {appetizer => None,       main => green salad,   drink => tea}:  200,
>     }
> 
> (syntax is only for illustration purposes)
> 
> Clearly the hash has to take the keys and values into account, which means 
> that both the keys and
> values have to be frozen.
> 
> (Values may be mutable objects, but then the frozendict can't be hashed -- 
> just like tuples can't be
> hashed if any item in them is mutable.)

Right, but that doesn't mean you have to require that values are hashable.

A frozendict could (and probably should) use the same logic as tuples:
if the values are hashable, the frozendict is hashable, otherwise not.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Feb 28 2012)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________
2012-02-13: Released eGenix pyOpenSSL 0.13        http://egenix.com/go26
2012-02-09: Released mxODBC.Zope.DA 2.0.2         http://egenix.com/go25
2012-02-06: Released eGenix mx Base 3.2.3         http://egenix.com/go24

::: Try our new mxODBC.Connect Python Database Interface for free ! ::::


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to