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].

678 questions
2418
votes
11 answers

Why is "1000000000000000 in range(1000000000000001)" so fast in Python 3?

It is my understanding that the range() function, which is actually an object type in Python 3, generates its contents on the fly, similar to a generator. This being the case, I would have expected the following line to take an inordinate amount of…
Rick supports Monica
  • 33,838
  • 9
  • 54
  • 100
1100
votes
14 answers

How does the @property decorator work in Python?

I would like to understand how the built-in function property works. What confuses me is that property can also be used as a decorator, but it only takes arguments when used as a built-in function and not when used as a decorator. This example is…
ashim
  • 20,778
  • 27
  • 68
  • 89
889
votes
11 answers

Usage of __slots__?

What is the purpose of __slots__ in Python — especially with respect to when I would want to use it, and when not?
Jeb
  • 12,898
  • 6
  • 31
  • 36
567
votes
5 answers

Are dictionaries ordered in Python 3.6+?

Dictionaries are ordered in Python 3.6 (under the CPython implementation at least) unlike in previous incarnations. This seems like a substantial change, but it's only a short paragraph in the documentation. It is described as a CPython…
Chris_Rands
  • 30,797
  • 12
  • 66
  • 100
554
votes
11 answers

"is" operator behaves unexpectedly with integers

Why does the following behave unexpectedly in Python? >>> a = 256 >>> b = 256 >>> a is b True # This is an expected result >>> a = 257 >>> b = 257 >>> a is b False # What happened here? Why is this False? >>> 257 is 257 True …
Greg Hewgill
  • 828,234
  • 170
  • 1,097
  • 1,237
410
votes
22 answers

When is del useful in Python?

I can't really think of any reason why Python needs the del keyword (and most languages seem to not have a similar keyword). For instance, rather than deleting a variable, one could just assign None to it. And when deleting from a dictionary, a del…
Jason Baker
  • 171,942
  • 122
  • 354
  • 501
384
votes
7 answers

time.sleep -- sleeps thread or process?

In Python for *nix, does time.sleep() block the thread or the process?
Jeremy Dunck
  • 5,234
  • 5
  • 23
  • 29
288
votes
2 answers

Why are some float < integer comparisons four times slower than others?

When comparing floats to integers, some pairs of values take much longer to be evaluated than other values of a similar magnitude. For example: >>> import timeit >>> timeit.timeit("562949953420000.7 < 562949953421000") # run 1 million…
Alex Riley
  • 132,653
  • 39
  • 224
  • 205
277
votes
2 answers

Why is 'x' in ('x',) faster than 'x' == 'x'?

>>> timeit.timeit("'x' in ('x',)") 0.04869917374131205 >>> timeit.timeit("'x' == 'x'") 0.06144205736110564 Also works for tuples with multiple elements, both versions seem to grow linearly: >>> timeit.timeit("'x' in ('x',…
Markus Meskanen
  • 15,203
  • 11
  • 57
  • 106
253
votes
8 answers

What is the global interpreter lock (GIL) in CPython?

What is a global interpreter lock and why is it an issue? A lot of noise has been made around removing the GIL from Python, and I'd like to understand why that is so important. I have never written a compiler nor an interpreter myself, so don't be…
e-satis
  • 515,820
  • 103
  • 283
  • 322
251
votes
9 answers

Are tuples more efficient than lists in Python?

Is there any performance difference between tuples and lists when it comes to instantiation and retrieval of elements?
readonly
  • 306,152
  • 101
  • 198
  • 201
213
votes
9 answers

How is Python's List Implemented?

Is it a linked list, an array? I searched around and only found people guessing. My C knowledge isn't good enough to look at the source code.
Greg
  • 39,830
  • 86
  • 217
  • 286
196
votes
5 answers

Accessing class variables from a list comprehension in the class definition

How do you access other class variables from a list comprehension within the class definition? The following works in Python 2 but fails in Python 3: class Foo: x = 5 y = [x for i in range(1)] Python 3.2 gives the error: NameError: global…
Mark Lodato
  • 41,133
  • 5
  • 39
  • 31
190
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
  • 21,470
  • 25
  • 65
  • 97
166
votes
8 answers

Finding the source code for built-in Python functions?

Is there a way to see how built in functions work in python? I don't mean just how to use them, but also how were they built, what is the code behind sorted or enumerate etc...?
user1073865
  • 1,773
  • 2
  • 12
  • 7
1
2 3
45 46