760 questions
0 votes
2 answers
81 views
Python exec and Git's worktree lifecycle
I'm encountering a peculiar issue when using Python's exec function within scripts that are run across different Git worktrees. Specifically, I've observed that the behavior of exec (and by extension, ...
1 vote
0 answers
167 views
What is the documented behaviour (if any) when someone imports `pkg.__init__[ as pkg]` instead of `import pkg`?
To be clear, I'm not suggesting anyone actually should import pkg.__init__ directly. This is to understand potential pitfalls if someone decides to convert a module-only distribution into a package, ...
3 votes
1 answer
186 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
296 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
78 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
169 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
198 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
107 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
96 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
95 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
191 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
145 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
66 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 >...