| ▲ | sundarurfriend 2 days ago |
| 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 idea if there's inaccuracies in these). So could this also have been approached from the other side, as in making unordered NamedTuples with support for the Mapping API? The line between dictionaries and named tuples and structs (across various languages) has always seemed a bit blurry to me, so I'm trying to get a better picture of it all through this. |
|
| ▲ | quietbritishjim 2 days ago | parent | next [-] |
| > 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. |
|
| ▲ | _flux 2 days ago | parent | prev | next [-] |
| The key difference is what the GPT outlined: tuples have order for the fields and named tuples are not indexed by the name, but instead a field accessor is used, i.e. foo.bar vs foo["bar"]. In addition namedtuples can be indexed using that order like tuples can (foo[0]), which clearly isn't possible with dicts and would be quite confusing if dict had integer key. So I think the differences aren't great, but they are sufficient. A frozendict is not going to be indexable by an integer. Python already has an abstract type for this, for mostly the use of type checking I imagine: https://docs.python.org/3/glossary.html#term-mapping Documentation for namedtuple: https://docs.python.org/3/library/collections.html#collectio... |
|
| ▲ | minitech 2 days ago | parent | prev | next [-] |
| On top of generating types dynamically being slow and bad as quietbritishjim said, “str values that also happen to be valid identifiers” is a very limiting dict key requirement. |
| |
|
| ▲ | willvarfar 2 days ago | parent | prev | next [-] |
| 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. |
| |
| ▲ | pansa2 2 days ago | parent [-] | | > 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"). | | |
| ▲ | vscode-rest 2 days ago | parent [-] | | Tuple entries must be hashable, which (as far as standard library is concerned) means immutable. | | |
| ▲ | pansa2 2 days ago | parent [-] | | >>> hash([1, 2])
TypeError: unhashable type: 'list'
>>> t = ([1, 2], [3, 4])
>>> print(t)
([1, 2], [3, 4])
| | |
| ▲ | vscode-rest 2 days ago | parent [-] | | Ah. Of course… that’s how the workaround to use tuples as frozen dicts can work in the first place. Slow morning for me! |
|
|
|
|
|
| ▲ | grimgrin 2 days ago | parent | prev [-] |
| 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 |
| |
| ▲ | chistev 2 days ago | parent [-] | | This place hates ChatGPT and AI. Lol. Edit: Of course, I get down voted as I predicted I would. Lol. | | |
| ▲ | acdha 2 days ago | parent | next [-] | | 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. | | |
| ▲ | delaminator 2 days ago | parent [-] | | Pasting chat GPT responses is against the site rules. always has been even before GPT https://news.ycombinator.com/item?id=46206457 | | |
| ▲ | quietbritishjim a day ago | parent | next [-] | | True, but the original comment that we're talking about here (by sundarurfriend) just mentioned an LLM's output in passing as part of their (presumably) human-written comment. Nothing you've linked to prohibits that. | | |
| ▲ | delaminator a day ago | parent [-] | | Presaging your bot produced comment with "A bot said this" is not human written | | |
| ▲ | quietbritishjim 5 hours ago | parent | next [-] | | That is still true and still irrelevant here. The comment we're talking about was not written by a bot with a disclaimer at the start. They just asked about its output. They didn't even quote its output - they paraphrased it and added their own commentary! I know HN rules prohibit saying "did you even read it?" but you surely can't have read the comment to have come to this view, or at least significantly misread it. Have another look. Most of all, HN guidelines are about encouraging thoughtful discussion. sundarurfriend's comment asked a genuinely interesting question and inspired interesting discussion. This subthread of "but AI!" did not. | |
| ▲ | acdha a day ago | parent | prev [-] | | Except in that case they were summarizing it, which I read as closer to “I found this on Stack Overflow but don’t know if it’s right”. I think that’s less offensive than having the post be LLM output or, especially, pretending to be authoritative. |
|
| |
| ▲ | acdha 2 days ago | parent | prev [-] | | Fair, because they’re not your words. I’ll edit my comment for what I had in mind: that it can be helpful for that like a spell checker - for example, I know non-native English speakers who find them useful for editing but they completely understand the topic intellectually. |
|
| |
| ▲ | grimgrin 2 days ago | parent | prev [-] | | you made a non-comment, that's why you're downvoted comments don't have to be substantial, but they should be adding something that might merit more responses, or could. yours does not. what kind of reply could you even give to "this place [and so, its users] hates [thing]" I'd ask you to qualify it, but you can't really qualify such a statement |
|
|