Does Python3 offer a FrozenDict?

J

Johannes Bauer

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
 
B

bearophileHUGS

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
 
P

Paul Moore

Johannes Bauer:


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 :)
 
B

bearophileHUGS

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
 
S

Steven D'Aprano

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.
 
J

Johannes Bauer

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

tuple(sorted(some_dict.items))

may do the job.

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

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

Regards,
Johannes
 
J

Johannes Bauer

Johannes Bauer:

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.

Regards,
Johannes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top