python-internals

What is the `ExceptionTable` in the output of `dis`?

What is the `ExceptionTable` in the output of `dis`? Question: In python3.13, when I try to disassemble [i for i in range(10)], the result is as below: >>> import dis >>> >>> dis.dis(‘[i for i in range(10)]’) 0 RESUME 0 1 LOAD_NAME 0 (range) PUSH_NULL LOAD_CONST 0 (10) CALL 1 GET_ITER LOAD_FAST_AND_CLEAR 0 (i) SWAP …

Total answers: 1

Why is b.pop(0) over 200 times slower than del b[0] for bytearray?

Why is b.pop(0) over 200 times slower than del b[0] for bytearray? Question: Letting them compete three times (a million pops/dels each time): from timeit import timeit for _ in range(3): t1 = timeit(‘b.pop(0)’, ‘b = bytearray(1000000)’) t2 = timeit(‘del b[0]’, ‘b = bytearray(1000000)’) print(t1 / t2) Time ratios (Try it online!): 274.6037053753368 219.38099365582403 252.08691226683823 …

Total answers: 3

Are Python 3.11 objects as light as slots?

Are Python 3.11 objects as light as slots? Question: After Mark Shannon’s optimisation of Python objects, is a plain object different from an object with slots? I understand that after this optimisation in a normal use case, the objects have no dictionary. Have the new Python objects made it unnecessary to use slots at all? …

Total answers: 1

Why PyList_Append is called each time a list is evaluated?

Why PyList_Append is called each time a list is evaluated? Question: I’m working with CPython3.11.0a3+. I added a break point at PyList_Append and modified the function to stop when the newitem is a dict. The original function: int PyList_Append(PyObject *op, PyObject *newitem) { if (PyList_Check(op) && (newitem != NULL)) return app1((PyListObject *)op, newitem); PyErr_BadInternalCall(); return …

Total answers: 1

How are small sets stored in memory?

How are small sets stored in memory? Question: If we look at the resize behavior for sets under 50k elements: >>> import sys >>> s = set() >>> seen = {} >>> for i in range(50_000): … size = sys.getsizeof(s) … if size not in seen: … seen[size] = len(s) … print(f"{size=} {len(s)=}") … s.add(i) …

Total answers: 1

Python binding environment on locals

Python binding environment on locals Question: In the following: a = 3 def b(): # global a; // will error without this statement a = a + 1 print a It will error unless I add a global. It seems in this sense that python evaluates the LHS of the expression first (creating a new …

Total answers: 1

Why are double curly braces used instead of backslash in python f-strings?

Why are double curly braces used instead of backslash in python f-strings? Question: We usually use the backslash to escape illegal characters. For example, escaping the double quotes. >>> """ == ‘"’ True In f-strings, curly braces are used for placeholding. To represent a curly brace, the braces are doubled. For example, >>> f"{{}}" == …

Total answers: 2

What causes [*a] to overallocate?

What causes [*a] to overallocate? Question: 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 …

Total answers: 3