Last active
October 10, 2016 12:33
-
-
Save mirekfranc/61e40a183171b9c123f4f40a45e99693 to your computer and use it in GitHub Desktop.
access to the last element of an array in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> a = [1, 2, 3] | |
>>> import dis | |
>>> def f(): | |
print a[len(a)-1] | |
... ... | |
>>> def g(): | |
print a[-1] | |
... ... | |
>>> dis.dis(f) | |
2 0 LOAD_GLOBAL 0 (a) | |
3 LOAD_GLOBAL 1 (len) | |
6 LOAD_GLOBAL 0 (a) | |
9 CALL_FUNCTION 1 | |
12 LOAD_CONST 1 (1) | |
15 BINARY_SUBTRACT | |
16 BINARY_SUBSCR | |
17 PRINT_ITEM | |
18 PRINT_NEWLINE | |
19 LOAD_CONST 0 (None) | |
22 RETURN_VALUE | |
>>> dis.dis(g) | |
2 0 LOAD_GLOBAL 0 (a) | |
3 LOAD_CONST 1 (-1) | |
6 BINARY_SUBSCR | |
7 PRINT_ITEM | |
8 PRINT_NEWLINE | |
9 LOAD_CONST 0 (None) | |
12 RETURN_VALUE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment