Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Does Python3 offer a FrozenDict?

56 views
Skip to first unread message

Johannes Bauer

unread,
Dec 16, 2008, 11:59:30 AM12/16/08
to
Hello group,

is there anything like a frozen dict in Python3, so I could do a

foo = { FrozenDict({"a" : "b"}): 3 }

or something like that?

Regards,
Johannes

--
"Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit,
verlästerung von Gott, Bibel und mir und bewusster Blasphemie."
-- Prophet und Visionär Hans Joss aka HJP in de.sci.physik
<48d8bf1d$0$7510$5402...@news.sunrise.ch>

bearoph...@lycos.com

unread,
Dec 16, 2008, 12:28:09 PM12/16/08
to
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/

Please remember that Py3 is very new, so 99% of the stuff present for
Py2.x is absent. Using Py3 is your choice.

Bye,
bearophile

Paul Moore

unread,
Dec 16, 2008, 12:56:57 PM12/16/08
to
On 16 Dec, 17:28, bearophileH...@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 :-)

bearoph...@lycos.com

unread,
Dec 16, 2008, 1:42:11 PM12/16/08
to
Paul Moore:

> Moral - don't assume that all code needs to be rewritten for Python
> 3.0 :-)

In practice this time your moral is of little use: having a place that
allows you to choose Py3 OR Py2 code is much better and tidier, helps
you save time, helps you avoid wasting some time, etc.

Bye,
bearophile

Steven D'Aprano

unread,
Dec 16, 2008, 8:26:58 PM12/16/08
to
On Tue, 16 Dec 2008 17:59:30 +0100, Johannes Bauer wrote:

> Hello group,
>
> is there anything like a frozen dict in Python3, so I could do a
>
> foo = { FrozenDict({"a" : "b"}): 3 }
>
> or something like that?


If *all* you want is to use it as a key, then:

tuple(sorted(some_dict.items))

may do the job.

--
Steven

Johannes Bauer

unread,
Dec 16, 2008, 11:50:50 PM12/16/08
to
Steven D'Aprano schrieb:

Well, I'd like to access it like a dict afterwards, e.g.

for (i, j) in foo.items():
print(i["a"])

Johannes Bauer

unread,
Dec 16, 2008, 11:51:45 PM12/16/08
to
bearoph...@lycos.com schrieb:

> 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/

Well, yes, I've been using something like that so far - but I think this
is functionality the language should provide - therefore I thought maybe
Python3 included something like this already.

0 new messages