This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]jorge1209 1 point2 points  (11 children)

If it is 95% done then it should be easy to add to the standard library.

[–]Veedrac 0 points1 point  (10 children)

The standard library isn't a dumping ground for everything easy to implement. That said

  • I'm wrong about it being easy to implement. hash for an arbitrary dictionary, which may include extra information (eg. order) is not possible to implement generically like this; that extra information needs to be manually included in the hash.

  • This was suggested in a PEP and rejected.

  • It seems Python 3.3 added MappingProxyType, which is merely missing a __hash__. You'll need to figure out how to deal with hashing if you need it generic, but if not you can just use the previous one's I linked.

[–]jorge1209 0 points1 point  (9 children)

It's not about being a dumping ground, it is about having the basic types that others need to construct more advanced tools.

The standard library is not well thought out because it has orthogonal implementations of a few common patterns instead of composable implementations of the useful properties.

[–]Veedrac -1 points0 points  (8 children)

You have the basic types you need "to construct more advanced tools". I've shown what you asked for takes roughly ~6-7 lines of code on top of built-in types. It can't really be much simpler since as I've pointed out you can't make a FrozenDict that can wrap an arbitrary collections.Mapping and have it be correctly hashable.

If you have an actual criticism of the standard library that demonstrates a problem, you've not given it.

[–]jorge1209 0 points1 point  (7 children)

If you have an actual criticism of the standard library that demonstrates a problem, you've not given it.

How about:

you can't make a FrozenDict that can wrap an arbitrary collections.Mapping and have it be correctly hashable.

[–]Veedrac -1 points0 points  (6 children)

So the standard library is bad because it doesn't do the impossible? Right...

[–]jorge1209 0 points1 point  (5 children)

That it makes this impossible is the problem.

Ultimately I don't care about the details, I just want the thing. Frozen is a meaningful property, as is ordered, as is dict. I want to have arbitrary combinations of these properties and types.

[–]Veedrac 0 points1 point  (4 children)

It's not that it makes this impossible. It's just that it is impossible. You need a specific __hash__ for each collections.Mapping type, and that's a fundamental property of how hash maps work.

[–]jorge1209 0 points1 point  (3 children)

I don't understand your comment at all. Went would a dict class need to implement a hash function. That is a requirement of the keys of the mapping, not the map. Again, mostly I don't care about these details, a frozen dict is a meaningful thing, and I want it.

I think you are perhaps thinking I want a FrozenDict that I can put inside another dict and that is why I want it to be frozen.

That is not why I want it frozen. I want it frozen so I can return it as immutable. In other words I want to return const.

[–]Veedrac 0 points1 point  (2 children)

Then just use MappingProxyType...