Skip to content

Instantly share code, notes, and snippets.

@lucaswiman
Created June 20, 2022 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucaswiman/5fa7b78a872c5e3045a0afaa3e2b4c19 to your computer and use it in GitHub Desktop.
Save lucaswiman/5fa7b78a872c5e3045a0afaa3e2b4c19 to your computer and use it in GitHub Desktop.
islice versus destructuring dis
>>> import dis
>>> from itertools import islice
>>> def first_two_islice(it):
... return tuple(islice(it, 2))
...
>>> def first_two_destructuring(it):
... x, y, *rest = it
... return x, y
...
>>> dis.dis(first_two_islice)
2 0 LOAD_GLOBAL 0 (tuple)
2 LOAD_GLOBAL 1 (islice)
4 LOAD_FAST 0 (it)
6 LOAD_CONST 1 (2)
8 CALL_FUNCTION 2
10 CALL_FUNCTION 1
12 RETURN_VALUE
>>> dis.dis(first_two_destructuring)
2 0 LOAD_FAST 0 (it)
2 UNPACK_EX 2
4 STORE_FAST 1 (x)
6 STORE_FAST 2 (y)
8 STORE_FAST 3 (rest)
3 10 LOAD_FAST 1 (x)
12 LOAD_FAST 2 (y)
14 BUILD_TUPLE 2
16 RETURN_VALUE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment