Python sets, frozensets, and literals
Python sets, frozensets, and literals
Posted Jan 19, 2022 18:35 UTC (Wed) by JoeBuck (subscriber, #2330)Parent article: Python sets, frozensets, and literals
Posted Jan 20, 2022 5:48 UTC (Thu)
by wtanksleyjr (subscriber, #74601)
[Link] (3 responses)
Posted Jan 20, 2022 11:13 UTC (Thu)
by quietbritishjim (subscriber, #114117)
[Link] (2 responses)
By itself that isn't a problem. If you create a set, put it somewhere it needs to be frozen (key of a dict, inside another set, etc.), and try to modify it then you would get an error that it's frozen. Well, that's OK, the developer gets an error that they can't modify it because it's being used in a context where it needs to be frozen.
The real problem is that you can have multiple references to the same object. So I put my set into a variable X, then also put it into a context where it needs to be frozen. If you copy the set into a frozenset then changes to X won't be reflected in the new location - very confusing. If the set object itself magically morphed into a frozenset (which I think is what the parent comment meant), then modifications via X now fail. This is particularly a problem if this is "someone else's" set. So a set gets passed as a parameter to a function, then that function accidentially turns it to a frozenset by using it in some other data structure. The caller might get an error in a substantially later part of the code, and it will be hard to find the function call that froze the set.
Posted Jan 20, 2022 18:48 UTC (Thu)
by JoeBuck (subscriber, #2330)
[Link] (1 responses)
But it probably isn't a good idea, because it could be unexpectedly expensive: people expect hashes to be fast, and if we're always doing copy-and-insert, copy-and-hash it would be costly.
Posted Jan 20, 2022 23:08 UTC (Thu)
by quietbritishjim (subscriber, #114117)
[Link]
> If you copy the set into a frozenset then changes to X won't be reflected in the new location - very confusing.
I think the fact that changes to the original set won't be reflected in the frozenset made from it earlier is even more serious than the cost of the copy.
Python sets, frozensets, and literals
Python sets, frozensets, and literals
Python sets, frozensets, and literals
Python sets, frozensets, and literals