Live data from Hacker News

A “frozen” dictionary for Python

lwn.net

21–30 of 172 posts

Re: A “frozen” dictionary for Python

#21
SQLAlchemy has its own frozendict which we've had in use for many years, we have it as a pretty well performing cython implementation these days, and I use it ubiquitously. Would be a very welcome addition to the stdlib.

This proposal is important enough that I chimed in on the thread with a detailed example of how SQLAlchemy uses this pattern:

https://discuss.python.org/t/pep-814-add-frozendict-built-in...

Re: A “frozen” dictionary for Python

#22

Can someone ELI5 the core difference between this and named tuples, for someone who is not deep into Python? ChatGPT's answer boiled down to: unordered (this) vs ordered (NTs), "arbitrary keys, decided at runtime" vs "fixed set of fields decided at definition time" (can't an NT's keys also be interpolated from runtime values?), and a different API (`.keys()`, `.items()`), etc (I'm just giving this as context btw, no…

> arbitrary keys, decided at runtime" vs "fixed set of fields decided at definition time" (can't an NT's keys also be interpolated from runtime values?)

If you want to create a named tuple with arbitrary field names at runtime then you need to create a new named tuple type before you create the instance. That is possible, since Python is a very dynamic language, but it's not particularly efficient and, more importantly, just feels a bit wrong. And are you going to somehow cache the types and reuse them if the field names match? It's all a bit of a mess compared to just passing the entries to the frozendict type.

Re: A “frozen” dictionary for Python

#23

Can someone ELI5 the core difference between this and named tuples, for someone who is not deep into Python? ChatGPT's answer boiled down to: unordered (this) vs ordered (NTs), "arbitrary keys, decided at runtime" vs "fixed set of fields decided at definition time" (can't an NT's keys also be interpolated from runtime values?), and a different API (`.keys()`, `.items()`), etc (I'm just giving this as context btw, no…

The values in tuples cannot change. The values that keys point to in a frozen dict can?

But yeah I'd be in favour of something that looked a lot like a named tuple but with mutable values and supporting [name] access too. And of course some nice syntactic sugar rather like dicts and sets have with curly brackets today.

Re: A “frozen” dictionary for Python

#24

Can someone ELI5 the core difference between this and named tuples, for someone who is not deep into Python? ChatGPT's answer boiled down to: unordered (this) vs ordered (NTs), "arbitrary keys, decided at runtime" vs "fixed set of fields decided at definition time" (can't an NT's keys also be interpolated from runtime values?), and a different API (`.keys()`, `.items()`), etc (I'm just giving this as context btw, no…

I think you could have asked this same comment w/o mentioning ChatGPT and you wouldn't have been downvoted to oblivion in 3 minutes I don't see anything wrong with your asking to understand

This place hates ChatGPT and AI. Lol.

Edit: Of course, I get down voted as I predicted I would. Lol.

Re: A “frozen” dictionary for Python

#25
post #24

Earlier quoted context omitted.

I think you could have asked this same comment w/o mentioning ChatGPT and you wouldn't have been downvoted to oblivion in 3 minutes I don't see anything wrong with your asking to understand

This place hates ChatGPT and AI. Lol. Edit: Of course, I get down voted as I predicted I would. Lol.

This place hates laziness and imprecision. Using ChatGPT for editing or inspiration is okay as long as you personally review the results for accuracy and completeness, at which point people care about it as much as you announcing that you used a spell checker.

Re: A “frozen” dictionary for Python

#26

Can someone ELI5 the core difference between this and named tuples, for someone who is not deep into Python? ChatGPT's answer boiled down to: unordered (this) vs ordered (NTs), "arbitrary keys, decided at runtime" vs "fixed set of fields decided at definition time" (can't an NT's keys also be interpolated from runtime values?), and a different API (`.keys()`, `.items()`), etc (I'm just giving this as context btw, no…

The values in tuples cannot change. The values that keys point to in a frozen dict can? But yeah I'd be in favour of something that looked a lot like a named tuple but with mutable values and supporting [name] access too. And of course some nice syntactic sugar rather like dicts and sets have with curly brackets today.

> The values in tuples cannot change. The values that keys point to in a frozen dict can?

The entries of a tuple cannot be assigned to, but the values can be mutated. The same is true for a `frozendict` (according to the PEP they don't support `__setitem__`, but "values can be mutable").

Re: A “frozen” dictionary for Python

#27
post #8

Great! Now make `set` have a stable order and we're done here.

Aren’t sets unsorted by definition? Or do repeated accesses without modification yield different results?

So are dictionary keys, but Python decided to make them insertion ordered (after having them be unordered just like set elements for decades). There is no fundamental reason sets couldn't have a defined order. That's what languages like JavaScript have done too.

Re: A “frozen” dictionary for Python

#28
post #25
post #24

Earlier quoted context omitted.

This place hates ChatGPT and AI. Lol. Edit: Of course, I get down voted as I predicted I would. Lol.

This place hates laziness and imprecision. Using ChatGPT for editing or inspiration is okay as long as you personally review the results for accuracy and completeness, at which point people care about it as much as you announcing that you used a spell checker.

Pasting chat GPT responses is against the site rules.

always has been even before GPT

https://news.ycombinator.com/item?id=46206457

Re: A “frozen” dictionary for Python

#29
post #8

Great! Now make `set` have a stable order and we're done here.

Aren’t sets unsorted by definition? Or do repeated accesses without modification yield different results?

this is likely in reference to the fact that dicts have maintained insertion order since Python ~3.6 as property of the language. Mathematically there's no defined order to a set, and a dict is really just a set in disguise, but it's very convenient for determinism to "add" this invariant to the language.
Post reply on HN