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
2964
votes
12 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
  • 43,029
  • 15
  • 76
  • 119
1333
votes
15 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
  • 24,380
  • 29
  • 72
  • 96
1185
votes
14 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
  • 15,939
  • 6
  • 34
  • 37
736
votes
6 answers

Are dictionaries ordered in Python 3.6+?

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…
Chris_Rands
  • 38,994
  • 14
  • 83
  • 119
616
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
  • 951,095
  • 183
  • 1,149
  • 1,285
464
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
  • 192,085
  • 135
  • 376
  • 510
418
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,724
  • 6
  • 25
  • 30
322
votes
6 answers

How does asyncio actually work?

This question is motivated by my another question: How to await in cdef? There are tons of articles and blog posts on the web about asyncio, but they are all very superficial. I couldn't find any information about how asyncio is actually…
wvxvw
  • 8,089
  • 10
  • 32
  • 61
313
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
  • 343,444
  • 107
  • 203
  • 205
295
votes
10 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
  • 45,306
  • 89
  • 231
  • 297
290
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
  • 169,130
  • 45
  • 262
  • 238
281
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
  • 19,939
  • 18
  • 80
  • 119
276
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…
Bite code
  • 578,959
  • 113
  • 301
  • 329
242
votes
10 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
  • 50,015
  • 5
  • 41
  • 32
227
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
  • 2,383
  • 3
  • 15
  • 7
1
2 3
48 49