Does Python3 offer a FrozenDict?
Paul Moore
p.f.moore at gmail.com
Tue Dec 16 12:56:57 EST 2008
On 16 Dec, 17:28, bearophileH... at lycos.com wrote:
> Johannes Bauer:
>
> > is there anything like a frozen dict in Python3, so I could do a
> > foo = { FrozenDict({"a" : "b"}): 3 }
>
> You can adapt this code to Python3 (and post a new recipe? It may be
> positive to create a new section of the Cookbook for Py3 only):http://code.activestate.com/recipes/414283/
There's actually only tiny changes needed (I believe)
- Change exception syntax: raise AttributeError("A frozendict cannot
be modified.")
- (bugfix for the original version): add **kw arg to __new__ (passed
to dict.__init__)
Simple testing looks OK. And the resulting code is OK for both 2.x and
3.0.
Moral - don't assume that all code needs to be rewritten for Python
3.0 :-)
Paul.
PS I imagine that 2to3 would have fixed this up fine, but it was so
easy to do by hand that I didn't bother :-)
More information about the Python-list
mailing list