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>
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
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 :-)
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
> 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
Well, I'd like to access it like a dict afterwards, e.g.
for (i, j) in foo.items():
print(i["a"])
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.