758 questions
3
votes
1
answer
151
views
What is the length of a python bytecode instruction in CPython?
Python docs on the dis module state that the length of a python bytecode instruction in CPython is 2 bytes (https://docs.python.org/3/library/dis.html)
However, when I disassemble a function and look ...
6
votes
2
answers
287
views
Is L[a:b]=L[c:d] optimized in Python to avoid creating a new list?
I am unable to find anything on the official Python documentation whether
L[a:b] = L[c:d]
creates a new (temporary) list for L[c:d] before the in-place modification of L. The tutorial says that:
All ...
1
vote
0
answers
77
views
How is import os.path possible? [duplicate]
Since os is a module instead of a package, import os.path should fail. For comparison:
>>> import os.sys
Traceback (most recent call last):
File "<python-input-0>", line 1, ...
2
votes
2
answers
159
views
How is `self` accessed in Python methods?
I am wondering how self works under-the-hood in Python classes.
My current limited understanding is that when a class is defined, e.g.
class Foo:
def __init__(self, x: int):
self.x = x
...
1
vote
1
answer
195
views
How can I store ids in Python without paying the 28-byte-per-int price?
My Python code stores millions of ids in various data structures, in order to implement a classic algorithm. The run time is good, but the memory usage is awful.
These ids are ints. I assume that ...
0
votes
1
answer
101
views
Why does Python pass an instance to a class attribute that is a function?
I define a class attribute and give it a function. When I call this function, the instance is passed on as the first argument (as if it's an instance function call with a self).
I would not expect an ...
2
votes
2
answers
93
views
Performance impact of inheriting from many classes
I am investigating the performance impact of a very broad inheritance setup.
Start with 260 distinct attribute names, from a0 through z9.
Create 260 classes with 1 uniquely-named attribute each. ...
21
votes
1
answer
2k
views
How/why are {2,3,10} and {x,3,10} with x=2 ordered differently?
Sets are unordered, or rather their order is an implementation detail. I'm interested in that detail. And I saw a case that surprised me:
print({2, 3, 10})
x = 2
print({x, 3, 10})
Output (Attempt ...
0
votes
0
answers
93
views
Dynamically create modules inside __init__ if they don't exist
I would like to dynamically create and import modules inside an inner __init__.py file, if one or several of a set of indexed submodules doesn't exist.
I have a set of module layers, say;
top_module/
...
7
votes
1
answer
188
views
Why is bytes(lst) slower than bytearray(lst)?
With lst = [0] * 10**6 I get times like these:
5.4 ± 0.4 ms bytearray(lst)
5.6 ± 0.4 ms bytes(bytearray(lst))
13.1 ± 0.7 ms bytes(lst)
Python:
3.13.0 (main, Nov 9 2024, 10:04:25) [GCC 14.2.1 ...
0
votes
1
answer
127
views
Why is `co_code_adaptive` in `PyCodeObject` cast to a `uint16_t` pointer in `_PyCode_CODE` macro?
I was exploring the Python 3.11 source code and came across the _PyCode_CODE macro. I noticed that within this macro, the co_code_adaptive member of PyCodeObject is cast to a uint16_t* pointer. ...
37
votes
1
answer
2k
views
In Python 3.12, why does 'Öl' take less memory than 'Ö'?
I just read PEP 393 and learned that Python's str type uses different internal representations, depending on the content. So, I experimented a little bit and was a bit surprised by the results:
>&...
0
votes
0
answers
58
views
Why is the SWAP instruction missing in Python 3.11's disassembled bytecode for tuple swaps?
In newer versions, ROT_TWO is replaced by SWAP.
However, after disassembling the function using dis.dis(), I noticed that SWAP does not appear in the bytecode.
Python 3.9.0
>>> import dis
>...
4
votes
0
answers
208
views
AsyncIO CPython hangs with 100% CPU usage
Our Python application is hanging on these 2 particular machines after 10-20 minutes of use. Htop shows 100% CPU usage. I used Pystack to get the stack trace of the running process. The Python side of ...
3
votes
1
answer
132
views
What is the internal implementation of `copy.deepcopy()` in Python and how to override `__deepcopy__()` correctly?
When reading Antony Hatchkins' answer to "How to override the copy/deepcopy operations for a Python object?", I am confused about why his implementation of __deepcopy()__ does not check memo ...