python-internals

How does __slots__ avoid a dictionary lookup?

寵の児 提交于 2019-11-27 02:41:04
问题 I've heard that __slots__ makes objects faster by avoiding a dictionary lookup. My confusion comes from Python being a dynamic language. In a static language, we avoid a dictionary lookup for a.test by doing a compile-time optimisation to save the index in the instruction we run. Now, in Python, a could just as easily be another object that has a dictionary or a different set of attributes. It seems like we'll still have to do a dictionary lookup - the only difference seems to be that we only

Why does list ask about __len__?

限于喜欢 提交于 2019-11-27 02:03:19
问题 class Foo: def __getitem__(self, item): print('getitem', item) if item == 6: raise IndexError return item**2 def __len__(self): print('len') return 3 class Bar: def __iter__(self): print('iter') return iter([3, 5, 42, 69]) def __len__(self): print('len') return 3 Demo: >>> list(Foo()) len getitem 0 getitem 1 getitem 2 getitem 3 getitem 4 getitem 5 getitem 6 [0, 1, 4, 9, 16, 25] >>> list(Bar()) iter len [3, 5, 42, 69] Why does list call __len__ ? It doesn't seem to use the result for anything

Accessing dictionary items by position in Python 3.6+ efficiently

空扰寡人 提交于 2019-11-27 01:57:32
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( n ) time complexity. Since getting an item from a list has O(1) complexity, is there a way to achieve

How does str(list) work?

雨燕双飞 提交于 2019-11-27 01:54:50
问题 Why does str(list) returns how we see list on the console? How does str(list) work? (any reference to the CPython code for str(list) )? >>> x = ['abc', 'def', 'ghi'] >>> str(x) "['abc', 'def', 'ghi']" To get the original list back from the str(list) I have to: >>> from ast import literal_eval >>> x = ['abc', 'def', 'ghi'] >>> str(x) "['abc', 'def', 'ghi']" >>> list(str(x)) ['[', "'", 'a', 'b', 'c', "'", ',', ' ', "'", 'd', 'e', 'f', "'", ',', ' ', "'", 'g', 'h', 'i', "'", ']'] >>> literal

Are sets ordered like dicts in python3.6

与世无争的帅哥 提交于 2019-11-27 01:33:35
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 detail and should not be relied upon No, set s are still unordered. You can verify this just by displaying a

Why can I use the same name for iterator and sequence in a Python for loop?

谁说胖子不能爱 提交于 2019-11-27 00:32:49
问题 This is more of a conceptual question. I recently saw a piece of code in Python (it worked in 2.7, and it might also have been run in 2.5 as well) in which a for loop used the same name for both the list that was being iterated over and the item in the list, which strikes me as both bad practice and something that should not work at all. For example: x = [1,2,3,4,5] for x in x: print x print x Yields: 1 2 3 4 5 5 Now, it makes sense to me that the last value printed would be the last value

What exactly is contained within a obj.__closure__?

二次信任 提交于 2019-11-27 00:31:26
Beazley pg 100 mentions: >>>python.__closure__ (<cell at 0x67f50: str object at 0x69230>,) >>>python.__closure__[0].cell_contents my understanding is that __closure__ is a list but what's all this cell stuff and str object?? That looks like a 1-ary tuple? Closure cells refer to values needed by the function but are taken from the surrounding scope. When Python compiles a nested function, it notes any variables that it references but are only defined in a parent function (not globals) in the code objects for both the nested function and the parent scope. These are the co_freevars and co

Improving performance of very large dictionary in Python

痴心易碎 提交于 2019-11-27 00:03:10
问题 I find that if I initialize an empty dictionary at the beginning, and then adding elements to the dictionary in a for loop (about 110,000 keys, the value for each key is a list, also increasing in the loop), the speed goes down as for loop goes. I suspect that the problem is, the dictionary does not know the number of keys at init time and it is not doing something very smart, so perhaps the storage collision becomes quite often and it slows down. If I know the number of keys and exactly what

Why is 'new_file += line + string' so much faster than 'new_file = new_file + line + string'? [duplicate]

情到浓时终转凉″ 提交于 2019-11-26 23:09:35
问题 This question already has an answer here: Why is variable1 += variable2 much faster than variable1 = variable1 + variable2? 1 answer Our code takes 10 minutes to siphon thru 68,000 records when we use: new_file = new_file + line + string However when we do the following it takes just 1 second: new_file += line + string Here is the code: for line in content: import time import cmdbre fname = "STAGE050.csv" regions = cmdbre.regions start_time = time.time() with open(fname) as f: content = f

How is the __name__ variable in a Python module defined?

ⅰ亾dé卋堺 提交于 2019-11-26 22:57:42
问题 I'm aware of the standard example: if you execute a module directly then it's __name__ global variable is defined as "__main__" . However, nowhere in the documentation can I find a precise description of how __name__ is defined in the general case. The module documentation says... Within a module, the module's name (as a string) is available as the value of the global variable __name__ . ...but what does it mean by "the module's name"? Is it just the name of the module (the filename with .py