759 questions
1
vote
0
answers
152
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, ...
6
votes
2
answers
290
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 ...
2
votes
2
answers
166
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
...
0
votes
1
answer
105
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 ...
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 ...
7
votes
1
answer
190
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 ...
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:
>&...
4
votes
0
answers
217
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 ...
0
votes
1
answer
411
views
Is there any way to construct a code object from a code string and assign it to an existing function using Python?
My problem is like this: I need to change how a function behave, but I can't access or change the file with which the function is located. I could import it though, and I would like to change the ...
1
vote
0
answers
80
views
How does the Python Interpreter check thread duration?
My understanding is that historically, the python Interpreter counted lines of code executed and switched threads after a fixed amount. This was then changed to being time dependent.
What I am trying ...
5
votes
2
answers
250
views
Why is `if x is None: pass` faster than `x is None` alone?
Timing results in Python 3.12 (and similar with 3.11 and 3.13 on different machines):
When x = None:
13.8 ns x is None
10.1 ns if x is None: pass
When x = True:
13.9 ns x is None
11.1 ns if x is ...
63
votes
1
answer
10k
views
Why is the simpler loop slower?
Called with n = 10**8, the simple loop is consistently significantly slower for me than the complex one, and I don't see why:
def simple(n):
while n:
n -= 1
def complex(n):
while True:...
6
votes
1
answer
518
views
What does RESUME opcode actually do?
The documentation is not very informative (at least for me):
opcode:: RESUME (context)
A no-op. Performs internal tracing, debugging and optimization
checks.
The context oparand consists of two parts....
0
votes
1
answer
447
views
Which calls in Python may not call `__call__`?
The answer to my question may depend on the interpreter for the code although I'm not sure. If it does, then I would be happy to hear about any widely used Python interpreter, especially CPython ...
0
votes
1
answer
145
views
Why does Python recursion limit change depending on function?
I was testing stuff when I noticed that python's recursion limit doesn't seem to apply equally to all functions. I'm not sure why or how and couldn't find any documentation explaining this behavior.
...
0
votes
1
answer
648
views
Why does my Python thread block the main thread unless I add a print or a sleep?
Does anyone know why running this code causes the script to hang in the thread, unless I uncomment the print, the sleep, or the "if" condition, or remove the try/except? My understanding is ...
4
votes
1
answer
549
views
Why is set.remove so slow here?
(Extracted from another question.) Removing this set's 200,000 elements one by one like this takes 30 seconds (Attempt This Online!):
s = set(range(200000))
while s:
for x in s:
s.remove(x)...
2
votes
1
answer
621
views
Give an example/explanation of the closure parameter of the exec function
Can someone please explain to me when and how I would use the closure parameter of the exec function?
https://docs.python.org/3/library/functions.html#exec
The closure argument specifies a closure–a ...
1
vote
1
answer
461
views
How to clone a python class object? (not the instance but the class itself)
Imagine you have the following code:
class A:
pass
NewA = ... # copy A
NewA.__init__ = decorator(A.__init__) # but don't change A's init function, just NewA's
I am looking for a way to change ...
8
votes
1
answer
1k
views
What's the benefit of asyncio using weakrefs to keep track of tasks?
Python docs for asyncio.create_task state:
Important: Save a reference to the result of this function, to avoid a task disappearing mid-execution. The event loop only keeps weak references to tasks. ...
1
vote
1
answer
229
views
Prevent Python Interpreter from Exiting if CTRL-D is pressed
I am running a script with python -i main.py. The script starts some C threads and python threads using threading module, then python code ends and it goes to a prompt. How can i prevent python from ...
1
vote
1
answer
98
views
Local imports work in bundled PyInstaller app but in Python source
This issue has plagued me for the last few months, I need a more experienced opinion. We have a CLI Python application that uses a gRPC server to communicate with other backend services. Its ...
2
votes
1
answer
176
views
Python small integer cache: what's different when assigning multiple values?
I'm aware of the CPython implementation that holds a small integer cache in the [-5, 256] range, so I understand that a=2 and b=2 will refer to the same memory address (thus causing a is b to return ...
13
votes
1
answer
907
views
How are small sets stored in memory?
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)...
3
votes
2
answers
1k
views
Please explain to me how does Python interpreter executes modules written in C/C++?
I'm trying to understand how it works. I know that Python interpreter translates python source code to byte code representation for a virtual machine (Python interpreter is a virtual machine) and ...