
On 21Jan2022 20:57, Steven D'Aprano <steve@pearwood.info> wrote:
On Fri, Jan 21, 2022 at 11:18:27AM +1100, Cameron Simpson wrote:
Paired with a __freeze__ dunder method, this applies to any type, not just sets. (Where appropriate of course.)
So:
|{1,2,3}| frozen set |[1,2,3]| tuple! |any-iterable| tuple! |{1:2, 3:4}| frozen dict
A frozen "any iterable" is not necessarily a tuple.
Yeah, I had misgivings myself. I can imagine the freeze operator falling back to iteration if there's no __freeze__dunder (like bool falls back to length). I can equally imagine just raising a TypeError for no __freeze__. More inclined to the latter on reflection - iterators get consumed.
For example, a frozen binary tree should probably keep the tree structure and methods; a frozen dict.keys() should be a frozen set;
Sure. According to their __freeze__.
and its not clear what a frozen iterator should do. Should it run the iterator to exhaustion? Seems odd. Hence thinking freezing an iterator might just make a tuple.
What about non-collections? What's a frozen re.MatchObject?
A type error. Let's not get insane. The idea is a generic operator, but not everything can be used with such an operator. Cheers, Cameron Simpson <cs@cskk.id.au>