python-internals

Are tuples more efficient than lists in Python?

荒凉一梦 提交于 2019-11-26 00:57:04
问题 Is there any performance difference between tuples and lists when it comes to instantiation and retrieval of elements? 回答1: The dis module disassembles the byte code for a function and is useful to see the difference between tuples and lists. In this case, you can see that accessing an element generates identical code, but that assigning a tuple is much faster than assigning a list. >>> def a(): ... x=[1,2,3,4,5] ... y=x[2] ... >>> def b(): ... x=(1,2,3,4,5) ... y=x[2] ... >>> import dis >>>

'is' operator behaves unexpectedly with non-cached integers

前提是你 提交于 2019-11-26 00:33:55
问题 When playing around with the Python interpreter, I stumbled upon this conflicting case regarding the is operator: If the evaluation takes place in the function it returns True , if it is done outside it returns False . >>> def func(): ... a = 1000 ... b = 1000 ... return a is b ... >>> a = 1000 >>> b = 1000 >>> a is b, func() (False, True) Since the is operator evaluates the id() \'s for the objects involved, this means that a and b point to the same int instance when declared inside of

About Python's built in sort() method

有些话、适合烂在心里 提交于 2019-11-26 00:32:51
问题 What algorithm is the built in sort() method in Python using? Is it possible to have a look at the code for that method? 回答1: Sure! The code's here, starting with function islt and proceeding for QUITE a while;-). As Chris's comment suggests, it's C code. You'll also want to read this text file for a textual explanation, results, etc etc. If you prefer reading Java code than C code, you could look at Joshua Bloch's implementation of timsort in and for Java (Joshua's also the guy who

How does swapping of members in the python tuples (a,b)=(b,a) work internally?

流过昼夜 提交于 2019-11-26 00:15:48
问题 In [55]: a = 5 In [56]: b = 6 In [57]: (a, b) = (b, a) In [58]: a Out[58]: 6 In [59]: b Out[59]: 5 How does this swapping of values of a and b work internally? Its definitely not using a temp variable. 回答1: Python separates the right-hand side expression from the left-hand side assignment. First the right-hand side is evaluated, and the result is stored on the stack, and then the left-hand side names are assigned using opcodes that take values from the stack again. For tuple assignments with

Why is “1000000000000000 in range(1000000000000001)” so fast in Python 3?

丶灬走出姿态 提交于 2019-11-25 23:59:26
问题 It is my understanding that the range() function, which is actually an object type in Python 3, generates its contents on the fly, similar to a generator. This being the case, I would have expected the following line to take an inordinate amount of time, because in order to determine whether 1 quadrillion is in the range, a quadrillion values would have to be generated: 1000000000000000 in range(1000000000000001) Furthermore: it seems that no matter how many zeroes I add on, the calculation

Are tuples more efficient than lists in Python?

[亡魂溺海] 提交于 2019-11-25 23:41:19
Is there any performance difference between tuples and lists when it comes to instantiation and retrieval of elements? Mark Harrison The dis module disassembles the byte code for a function and is useful to see the difference between tuples and lists. In this case, you can see that accessing an element generates identical code, but that assigning a tuple is much faster than assigning a list. >>> def a(): ... x=[1,2,3,4,5] ... y=x[2] ... >>> def b(): ... x=(1,2,3,4,5) ... y=x[2] ... >>> import dis >>> dis.dis(a) 2 0 LOAD_CONST 1 (1) 3 LOAD_CONST 2 (2) 6 LOAD_CONST 3 (3) 9 LOAD_CONST 4 (4) 12

'order' of unordered Python sets

隐身守侯 提交于 2019-11-25 23:41:12
问题 I understand that sets in Python are unordered, but I\'m curious about the \'order\' they\'re displayed in, as it seems to be consistent. They seem to be out-of-order in the same way every time: >>> set_1 = set([5, 2, 7, 2, 1, 88]) >>> set_2 = set([5, 2, 7, 2, 1, 88]) >>> set_1 set([88, 1, 2, 5, 7]) >>> set_2 set([88, 1, 2, 5, 7]) ...and another example: >>> set_3 = set(\'abracadabra\') >>> set_4 = set(\'abracadabra\') >>> set_3 set([\'a\', \'r\', \'b\', \'c\', \'d\']) >>>> set_4 set([\'a\',

How is Python's List Implemented?

我是研究僧i 提交于 2019-11-25 23:30:33
问题 Is it a linked list, an array? I searched around and only found people guessing. My C knowledge isn\'t good enough to look at the source code. 回答1: It's a dynamic array. Practical proof: Indexing takes (of course with extremely small differences (0.0013 µsecs!)) the same time regardless of index: ...>python -m timeit --setup="x = [None]*1000" "x[500]" 10000000 loops, best of 3: 0.0579 usec per loop ...>python -m timeit --setup="x = [None]*1000" "x[0]" 10000000 loops, best of 3: 0.0566 usec

About the changing id of an immutable string

泪湿孤枕 提交于 2019-11-25 23:05:15
问题 Something about the id of objects of type str (in python 2.7) puzzles me. The str type is immutable, so I would expect that once it is created, it will always have the same id . I believe I don\'t phrase myself so well, so instead I\'ll post an example of input and output sequence. >>> id(\'so\') 140614155123888 >>> id(\'so\') 140614155123848 >>> id(\'so\') 140614155123808 so in the meanwhile, it changes all the time. However, after having a variable pointing at that string, things change: >>

Are dictionaries ordered in Python 3.6+?

余生长醉 提交于 2019-11-25 22:53:59
问题 Dictionaries are ordered in Python 3.6 (under the CPython implementation at least) unlike in previous incarnations. This seems like a substantial change, but it\'s only a short paragraph in the documentation. It is described as a CPython implementation detail rather than a language feature, but also implies this may become standard in the future. How does the new dictionary implementation perform better than the older one while preserving element order? Here is the text from the documentation