python-internals

Why is variable1 += variable2 much faster than variable1 = variable1 + variable2?

跟風遠走 提交于 2019-11-26 12:39:12
问题 I have inherited some Python code which is used to create huge tables (of up to 19 columns wide by 5000 rows). It took nine seconds for the table to be drawn on the screen. I noticed that each row was added using this code: sTable = sTable + \'\\n\' + GetRow() where sTable is a string. I changed that to: sTable += \'\\n\' + GetRow() and I noticed that the table now appeared in six seconds . And then I changed it to: sTable += \'\\n%s\' % GetRow() based on these Python performance tips (still

Why do two identical lists have a different memory footprint?

最后都变了- 提交于 2019-11-26 12:12:45
问题 I created two lists l1 and l2 , but each one with a different creation method: import sys l1 = [None] * 10 l2 = [None for _ in range(10)] print(\'Size of l1 =\', sys.getsizeof(l1)) print(\'Size of l2 =\', sys.getsizeof(l2)) But the output surprised me: Size of l1 = 144 Size of l2 = 192 The list created with a list comprehension is a bigger size in memory, but the two lists are identical in Python otherwise. Why is that? Is this some CPython internal thing, or some other explanation? 回答1: When

What are __signature__ and __text_signature__ used for in Python 3.4

蓝咒 提交于 2019-11-26 11:22:55
问题 If one does dir() on some builtin callables (class constructors, methods, etc) on CPython 3.4, one finds out that many of them often have a special attribute called __text_signature__ , for example: >>> object.__text_signature__ \'()\' >>> int.__text_signature__ >>> # was None However the documentation for this is nonexistent. Furthermore, googling for the attribute name suggests that there is also another possible special attribute __signature__ , though I did not find any built-in functions

How is tuple implemented in CPython?

五迷三道 提交于 2019-11-26 11:02:06
问题 I\'ve been trying to learn how CPython is implemented under the scenes. It\'s great that Python is high level, but I don\'t like treating it like a black box. With that in mind, how are tuples implemented? I\'ve had a look at the source (tupleobject.c), but it\'s going over my head. I see that PyTuple_MAXSAVESIZE = 20 and PyTuple_MAXFREELIST = 2000 , what is saving and the \"free list\"? (Will there be a performance difference between tuples of length 20/21 or 2000/2001? What enforces the

list comprehension filtering - “the set() trap”

最后都变了- 提交于 2019-11-26 11:00:05
问题 A reasonably common operation is to filter one list based on another list . People quickly find that this: [x for x in list_1 if x in list_2] is slow for large inputs - it\'s O(n*m). Yuck. How do we speed this up? Use a set to make filtering lookups O(1): s = set(list_2) [x for x in list_1 if x in s] This gives nice overall O(n) behavior. I however often see even veteran coders fall into The Trap ™: [x for x in list_1 if x in set(list_2)] Ack! This is again O(n*m) since python builds set(list

How references to variables are resolved in Python

半城伤御伤魂 提交于 2019-11-26 10:18:56
问题 This message is a a bit long with many examples, but I hope it will help me and others to better grasp the full story of variables and attribute lookup in Python 2.7. I am using the terms of PEP 227 (http://www.python.org/dev/peps/pep-0227/) for code blocks (such as modules, class definition, function definitions, etc.) and variable bindings (such as assignments, argument declarations, class and function declaration, for loops, etc.) I am using the terms variables for names that can be called

Why do ints require three times as much memory in Python?

做~自己de王妃 提交于 2019-11-26 09:55:58
问题 On a 64-bit system an integer in Python takes 24 bytes. This is 3 times the memory that would be needed in e.g. C for a 64-bit integer. Now, I know this is because Python integers are objects. But what is the extra memory used for? I have my guesses, but it would be nice to know for sure. 回答1: Remember that the Python int type does not have a limited range like C int has; the only limit is the available memory. Memory goes to storing the value, the current size of the integer storage (the

Accessing dictionary items by position in Python 3.6+ efficiently

旧城冷巷雨未停 提交于 2019-11-26 09:54:23
问题 I understand dictionaries are insertion ordered in Python 3.6+, as an implementation detail in 3.6 and official in 3.7+. Given they are ordered, it seems strange that no methods exist to retrieve the i th item of a dictionary by insertion order. The only solutions available appear to have O( n ) complexity, either: Convert to a list via an O( n ) process and then use list.__getitem__ . enumerate dictionary items in a loop and return the value when the desired index is reached. Again, with O(

How is unicode represented internally in Python?

给你一囗甜甜゛ 提交于 2019-11-26 09:48:37
问题 How is Unicode string literally represented in Python\'s memory? For example I could visualize \'abc\' as its equivalent ASCII bytes in Memory. Integer could be thought of as the 2\'s compliment representation. However u\'\\u2049\' , even though is represented in UTF-8 as \'\\xe2\\x81\\x89\' - 3 bytes long, how do I visualize the literal u\'\\u2049\' codepoint in the memory? Is there a specific way it is stored in memory? Does Python 2 and Python 3 treat it differently? Few related questions

Are sets ordered like dicts in python3.6

让人想犯罪 __ 提交于 2019-11-26 09:41:26
问题 Due to changes in dict implementation in Python 3.6 it is now ordered by default. Do set s preserve order as well now? I could not find any information about it but as both of those data structures are very similar in the way they work under the hood I thought it might be the case. I know there is no promise for dict s to be ordered in all cases but they are most of the time. As stated in Python docs: The order-preserving aspect of this new implementation is considered an implementation