Why operations between dict views return a set and not a frozenset?
Peter J. Holzer
hjp-python at hjp.at
Tue Jan 11 16:07:18 EST 2022
On 2022-01-11 19:49:20 +0100, Marco Sulla wrote:
> I think this is what you mean:
>
> >>> dis.dis("for _ in {1, 2}: pass")
> 1 0 SETUP_LOOP 12 (to 14)
> 2 LOAD_CONST 3 (frozenset({1, 2}))
> 4 GET_ITER
> >> 6 FOR_ITER 4 (to 12)
> 8 STORE_NAME 0 (_)
> 10 JUMP_ABSOLUTE 6
> >> 12 POP_BLOCK
> >> 14 LOAD_CONST 2 (None)
> 16 RETURN_VALUE
> >>> a = {1, 2}
> >>> dis.dis("for _ in a: pass")
> 1 0 SETUP_LOOP 12 (to 14)
> 2 LOAD_NAME 0 (a)
> 4 GET_ITER
> >> 6 FOR_ITER 4 (to 12)
> 8 STORE_NAME 1 (_)
> 10 JUMP_ABSOLUTE 6
> >> 12 POP_BLOCK
> >> 14 LOAD_CONST 0 (None)
> 16 RETURN_VALUE
I think you have omitted the part that Chris was hinting at.
>>> dis.dis("a = {1, 2};\nfor _ in a: pass")
1 0 LOAD_CONST 0 (1)
2 LOAD_CONST 1 (2)
4 BUILD_SET 2
6 STORE_NAME 0 (a)
2 8 LOAD_NAME 0 (a)
10 GET_ITER
>> 12 FOR_ITER 4 (to 18)
14 STORE_NAME 1 (_)
16 JUMP_ABSOLUTE 12
>> 18 LOAD_CONST 2 (None)
20 RETURN_VALUE
Now compare
2 LOAD_CONST 3 (frozenset({1, 2}))
with
1 0 LOAD_CONST 0 (1)
2 LOAD_CONST 1 (2)
4 BUILD_SET 2
and you see the difference between using a frozenset as a constant and
building a set at runtime.
hp
--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp at hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://mail.python.org/pipermail/python-list/attachments/20220111/3254e652/attachment.sig>
More information about the Python-list
mailing list