Skip to main content
Filter by
Sorted by
Tagged with
1 vote
0 answers
152 views

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, ...
dROOOze's user avatar
  • 4,194
6 votes
2 answers
290 views

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 ...
user29120650's user avatar
2 votes
2 answers
166 views

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 ...
FISR's user avatar
  • 293
0 votes
1 answer
105 views

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 ...
msrc's user avatar
  • 821
21 votes
1 answer
2k views

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 ...
Stefan Pochmann's user avatar
7 votes
1 answer
190 views

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 ...
Kelly Bundy's user avatar
37 votes
1 answer
2k views

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: >&...
Aemyl's user avatar
  • 2,447
4 votes
0 answers
217 views

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 ...
Sophon Aniketos's user avatar
0 votes
1 answer
411 views

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 ...
Luke L's user avatar
  • 328
1 vote
0 answers
80 views

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 ...
FourierFlux's user avatar
5 votes
2 answers
250 views

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 ...
no comment's user avatar
  • 10.9k
63 votes
1 answer
10k views

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:...
Kelly Bundy's user avatar
6 votes
1 answer
518 views

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....
Amir reza Riahi's user avatar
0 votes
1 answer
447 views

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 ...
Takuo Matsuoka's user avatar
0 votes
1 answer
145 views

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. ...
scr's user avatar
  • 1,022
0 votes
1 answer
648 views

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 ...
Nat's user avatar
  • 53
4 votes
1 answer
549 views

(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)...
Kelly Bundy's user avatar
2 votes
1 answer
621 views

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 ...
Ben Ellis's user avatar
  • 183
1 vote
1 answer
461 views

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 ...
Vahid Zee's user avatar
8 votes
1 answer
1k views

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. ...
max's user avatar
  • 52.7k
1 vote
1 answer
229 views

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 ...
Ahmed Eshra's user avatar
1 vote
1 answer
98 views

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 ...
wisenickel's user avatar
2 votes
1 answer
176 views

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 ...
Floella's user avatar
  • 1,397
13 votes
1 answer
907 views

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)...
wim's user avatar
  • 368k
3 votes
2 answers
1k views

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 ...
Viacheslav Prokopev's user avatar

1
2 3 4 5
16