python-internals

Why is x**4.0 faster than x**4 in Python 3?

Why is x**4.0 faster than x**4 in Python 3? Question: Why is x**4.0 faster than x**4? I am using CPython 3.5.2. $ python -m timeit “for x in range(100):” ” x**4.0″ 10000 loops, best of 3: 24.2 usec per loop $ python -m timeit “for x in range(100):” ” x**4″ 10000 loops, best of 3: …

Total answers: 3

Why is copying a shuffled list much slower?

Why is copying a shuffled list much slower? Question: Copying a shuffled range(10**6) list ten times takes me about 0.18 seconds: (these are five runs) 0.175597017661 0.173731403198 0.178601711594 0.180330912952 0.180811964451 Copying the unshuffled list ten times takes me about 0.05 seconds: 0.058402235973 0.0505464636856 0.0509734306934 0.0526022752744 0.0513324916184 Here’s my testing code: from timeit import timeit import …

Total answers: 4

Why does list ask about __len__?

Why does list ask about __len__? Question: 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 …

Total answers: 3

Is there a difference between [] and list() when using id()?

Is there a difference between [] and list() when using id()? Question: Can somebody explain the following? Why is the id the same, but the lists are different? >>> [] is [] False >>> id([]) == id([]) True Is there difference in list creation? >>> id(list()) == id(list()) False >>> id([]) == id([]) True Why …

Total answers: 1

Is there anything faster than dict()?

Is there anything faster than dict()? Question: 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 …

Total answers: 5

How big can the input to the input() function be?

How big can the input to the input() function be? Question: How large can the input I supply to the input() function be? Unfortunately, there was no easy way to test it. After using a lot of copy-pasting I couldn’t get input to fail on any input I supplied. (and I eventually gave up) The …

Total answers: 2

Understanding memory allocation for large integers in Python

Understanding memory allocation for large integers in Python Question: 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? …

Total answers: 2

Set literal gives different result from set function call

Set literal gives different result from set function call Question: Why does the set function call wipe out the dupes, but parsing a set literal does not? >>> x = Decimal(‘0’) >>> y = complex(0,0) >>> set([0, x, y]) {0} >>> {0, x, y} {Decimal(‘0’), 0j} (Python 2.7.12. Possibly same root cause as for this …

Total answers: 2

list() uses slightly more memory than list comprehension

list() uses slightly more memory than list comprehension Question: So i was playing with list objects and found little strange thing that if list is created with list() it uses more memory, than list comprehension? I’m using Python 3.5.2 In [1]: import sys In [2]: a = list(range(100)) In [3]: sys.getsizeof(a) Out[3]: 1008 In [4]: …

Total answers: 2

Are dictionaries ordered in Python 3.6+?

Are dictionaries ordered in Python 3.6+? Question: Dictionaries are insertion ordered as of Python 3.6. It is described as a CPython implementation detail rather than a language feature. The documentation states: dict() now uses a “compact” representation pioneered by PyPy. The memory usage of the new dict() is between 20% and 25% smaller compared to …

Total answers: 6