python-internals

Understanding memory allocation for large integers in Python

痴心易碎 提交于 2019-12-18 11:41:28
问题 How does Python allocate memory for large integers? An int type has a size of 28 bytes and as I keep increasing the value of the int , the size increases in increments of 4 bytes . Why 28 bytes initially for any value as low as 1 ? Why increments of 4 bytes ? PS: I am running Python 3.5.2 on a x86_64 (64 bit machine). Any pointers/resources/PEPs on how the (3.0+) interpreters work on such huge numbers is what I am looking for. Code illustrating the sizes: >>> a=1 >>> print(a.__sizeof__()) 28

Should importlib.reload restore a deleted attribute in Python 3.6?

情到浓时终转凉″ 提交于 2019-12-18 08:46:34
问题 I'm looking into these two related questions: here and here. I am seeing a behavior I do not expect in Python 3.6, which differs from behavior using plain reload in Python 2.7 (and 3.4). Namely, it seems that a module attribute that would be populated during module initialization or when re-exec-ing the module during a reload, is not restored after its local name is removed with del ... see below: For Python 3.6: In [1]: import importlib In [2]: import math In [3]: del math.cos In [4]: math

What happens when you try to delete a list element while iterating over it

£可爱£侵袭症+ 提交于 2019-12-18 05:07:50
问题 I'm iterating a list as follows: some_list = [1, 2, 3, 4] another_list = [1, 2, 3, 4] for idx, item in enumerate(some_list): del some_list[idx] for item in another_list: another_list.remove(item) When I print out the contents of the lists >>> some_list [2, 4] >>> another_list [2, 4] I'm aware that Python doesn't support modifying a list while iterating over it and the right way is to iterate over copy of list instead (so please don't downvote). But I want to know what exactly happens behind

Why is Python's 'len' function faster than the __len__ method?

风格不统一 提交于 2019-12-18 03:57:29
问题 In Python, len is a function to get the length of a collection by calling an object's __len__ method: def len(x): return x.__len__() So I would expect direct call of __len__() to be at least as fast as len() . import timeit setup = ''' ''' print (timeit.Timer('a="12345"; x=a.__len__()', setup=setup).repeat(10)) print (timeit.Timer('a="12345"; x=len(a)', setup=setup).repeat(10)) Demo link But results of testing with the above code shows len() to be faster. Why? 回答1: The builtin len() function

Why is Python's 'len' function faster than the __len__ method?

那年仲夏 提交于 2019-12-18 03:57:14
问题 In Python, len is a function to get the length of a collection by calling an object's __len__ method: def len(x): return x.__len__() So I would expect direct call of __len__() to be at least as fast as len() . import timeit setup = ''' ''' print (timeit.Timer('a="12345"; x=a.__len__()', setup=setup).repeat(10)) print (timeit.Timer('a="12345"; x=len(a)', setup=setup).repeat(10)) Demo link But results of testing with the above code shows len() to be faster. Why? 回答1: The builtin len() function

Why does `False is False is False` evaluate to `True`?

只谈情不闲聊 提交于 2019-12-17 18:22:01
问题 Why in Python it is evaluated this way: >>> False is False is False True but when tried with parenthesis is behaving as expected: >>> (False is False) is False False 回答1: Chaining operators like a is b is c is equivalent to a is b and b is c . So the first example is False is False and False is False , which evaluates to True and True which evaluates to True Having parenthesis leads to the result of one evaluation being compared with the next variable (as you say you expect), so (a is b) is c

Why does creating a list from a list make it larger?

浪子不回头ぞ 提交于 2019-12-17 17:59:45
问题 I'm seeing some inconsistencies when using sys.getsizeof on what should be identical lists. (Python 2.7.5) >>> lst = [0,1,2,3,4,5,6,7,8,9] >>> sys.getsizeof(lst) 76 >>> lst2 = list(lst) >>> sys.getsizeof(lst2) 104 >>> lst3 = list(lst2) >>> sys.getsizeof(lst3) 104 >>> sys.getsizeof(lst[:]) 76 >>> sys.getsizeof(lst2[:]) 76 Does anybody have a simple explanation? 回答1: With a list literal, the VM creates the list with a set length. When passing a sequence to the list() constructor the elements

Why is a class __dict__ a mappingproxy?

牧云@^-^@ 提交于 2019-12-17 17:59:31
问题 I wonder why a class __dict__ is a mappingproxy , but an instance __dict__ is just a plain dict >>> class A: ... pass >>> a = A() >>> type(a.__dict__) <class 'dict'> >>> type(A.__dict__) <class 'mappingproxy'> 回答1: This helps the interpreter assure that the keys for class-level attributes and methods can only be strings. Elsewhere, Python is a "consenting adults language", meaning that dicts for objects are exposed and mutable by the user. However, in the case of class-level attributes and

Is there anything faster than dict()?

醉酒当歌 提交于 2019-12-17 07:24:12
问题 I need a faster way to store and access around 3GB of k:v pairs. Where k is a string or an integer and v is an np.array() that can be of different shapes. Is there any object, that is faster than the standard python dict in storing and accessing such a table? For example, a pandas.DataFrame ? As far I have understood python dict is a quite fast implementation of a hashtable, is there anything better than that for my specific case? 回答1: No there is nothing faster than a dictionary for this

Tuple or list when using 'in' in an 'if' clause?

纵然是瞬间 提交于 2019-12-17 07:23:57
问题 Which approach is better? Using a tuple, like: if number in (1, 2): or a list, like: if number in [1, 2]: Which one is recommended for such uses and why (both logical and performance wise)? 回答1: The CPython interpreter replaces the second form with the first . That's because loading the tuple from a constant is one operation, but the list would be 3 operations; load the two integer contents and build a new list object. Because you are using a list literal that isn't otherwise reachable, it is