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.
|_|_) |                    |
| |   | h...@hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"

Attachment: signature.asc
Description: PGP signature

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to