Questions tagged [python-internals]

How does Python work underneath the hood? Use for questions relating to (for instance) the design decisions made and the internal data structures and algorithms used.

This tag is to be used for those posts relating to the internal working of code written in the programming language.

The scope of this tag includes the standard library [Python 2, Python 3].

732 questions
208
votes
8 answers

Why can tuples contain mutable items?

If a tuple is immutable then why can it contain mutable items? It is seemingly a contradiction that when a mutable item such as a list does get modified, the tuple it belongs to maintains being immutable.
qazwsx
  • 25,536
  • 30
  • 72
  • 106
170
votes
3 answers

Why do two identical lists have a different memory footprint?

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…
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
168
votes
5 answers

Why is the order in dictionaries and sets arbitrary?

I don't understand how looping over a dictionary or set in python is done by 'arbitrary' order. I mean, it's a programming language so everything in the language must be 100% determined, correct? Python must have some kind of algorithm that decides…
user1971598
165
votes
3 answers

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

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: 30.6 usec per…
arieljannai
  • 2,124
  • 3
  • 19
  • 39
163
votes
5 answers

Why are Python's arrays slow?

I expected array.array to be faster than lists, as arrays seem to be unboxed. However, I get the following result: In [1]: import array In [2]: L = list(range(100000000)) In [3]: A = array.array('l', range(100000000)) In [4]: %timeit sum(L) 1…
Valentin Lorentz
  • 9,556
  • 6
  • 47
  • 69
152
votes
4 answers

Is it possible to "hack" Python's print function?

Note: This question is for informational purposes only. I am interested to see how deep into Python's internals it is possible to go with this. Not very long ago, a discussion began inside a certain question regarding whether the strings passed to…
cs95
  • 379,657
  • 97
  • 704
  • 746
148
votes
3 answers

What causes [*a] to overallocate?

Apparently list(a) doesn't overallocate, [x for x in a] overallocates at some points, and [*a] overallocates all the time? Here are sizes n from 0 to 12 and the resulting sizes in bytes for the three methods: 0 56 56 56 1 64 88 88 2 72 88 96 3 80…
Stefan Pochmann
  • 27,593
  • 8
  • 44
  • 107
141
votes
10 answers

How to override the copy/deepcopy operations for a Python object?

I understand the difference between copy vs. deepcopy in the copy module. I've used copy.copy and copy.deepcopy before successfully, but this is the first time I've actually gone about overloading the __copy__ and __deepcopy__ methods. I've already…
Brent Writes Code
  • 19,075
  • 7
  • 52
  • 56
135
votes
2 answers

What algorithm does Python's built-in sort() method use?

What algorithm is the built in sort() method in Python using? Is it possible to have a look at the code for that method?
user89862
133
votes
3 answers

Why is it slower to iterate over a small string than a small list?

I was playing around with timeit and noticed that doing a simple list comprehension over a small string took longer than doing the same operation on a list of small single character strings. Any explanation? It's almost 1.35 times as much time. >>>…
Sunjay Varma
  • 5,007
  • 6
  • 34
  • 51
127
votes
4 answers

Why do tuples take less space in memory than lists?

A tuple takes less memory space in Python: >>> a = (1,2,3) >>> a.__sizeof__() 48 whereas lists takes more memory space: >>> b = [1,2,3] >>> b.__sizeof__() 64 What happens internally on the Python memory management?
JON
  • 1,668
  • 2
  • 15
  • 18
118
votes
3 answers

Why is a class __dict__ a mappingproxy?

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__) >>> type(A.__dict__)
José Luis
  • 3,713
  • 4
  • 33
  • 37
118
votes
2 answers

Python string interning

While this question doesn't have any real use in practice, I am curious as to how Python does string interning. I have noticed the following. >>> "string" is "string" True This is as I expected. You can also do this. >>> "strin"+"g" is…
Ze'ev G
  • 1,646
  • 2
  • 12
  • 15
116
votes
1 answer

Why is str.translate much faster in Python 3.5 compared to Python 3.4?

I was trying to remove unwanted characters from a given string using text.translate() in Python 3.4. The minimal code is: import sys s = 'abcde12345@#@$#%$' mapper = dict.fromkeys(i for i in range(sys.maxunicode) if chr(i) in…
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
113
votes
1 answer

What's with the integer cache maintained by the interpreter?

After dive into Python's source code, I find out that it maintains an array of PyInt_Objects ranging from int(-5) to int(256) (@src/Objects/intobject.c) A little experiment proves it: >>> a = 1 >>> b = 1 >>> a is b True >>> a = 257 >>> b = 257 >>> a…
felix021
  • 1,936
  • 3
  • 16
  • 20
1
2
3
48 49