Skip to main content
Filter by
Sorted by
Tagged with
1 vote
0 answers
162 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
3 votes
1 answer
179 views

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 ...
Petras Purlys's user avatar
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
1 vote
0 answers
77 views

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, ...
Aemyl's user avatar
  • 2,447
2 votes
2 answers
168 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
1 vote
1 answer
198 views

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 ...
SRobertJames's user avatar
  • 9,367
0 votes
1 answer
106 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
2 votes
2 answers
95 views

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. ...
Phrogz's user avatar
  • 304k
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
0 votes
0 answers
95 views

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/ ...
Skenvy's user avatar
  • 1,430
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
0 votes
1 answer
135 views

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. ...
stdiohero'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
0 votes
0 answers
66 views

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 >...
Jack's user avatar
  • 1
4 votes
0 answers
224 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
3 votes
1 answer
141 views

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 ...
Tom Lin's user avatar
  • 110
0 votes
1 answer
413 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
0 votes
0 answers
289 views

I have a utility package for Python projects I maintain and use. It’s a bundle of lightweight, common, junk-drawer tools (e.g. command parsing, app lifecycle management, document generation, &c &...
fish2000's user avatar
  • 4,435
1 vote
0 answers
84 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
8 votes
1 answer
593 views

>>> import sys >>> del sys.modules['sys'] >>> import sys >>> sys.modules Traceback (most recent call last): File "<stdin>", line 1, in <module&...
no step on snek'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
2 votes
1 answer
139 views

I want to know what the technical term for a colon in the context of introducing a suite after a statement is called. I do not mean colons in slices, key-value pairs or type hints, but this kind of ...
Blyfh's user avatar
  • 95
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
3 votes
1 answer
593 views

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 ...
Amir reza Riahi's user avatar
6 votes
1 answer
523 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
49 votes
3 answers
4k views

This is disassembly of a list comprehension in python-3.10: Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux Type "help", "copyright", "credits" or &...
Amir reza Riahi's user avatar
0 votes
1 answer
448 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
8 votes
2 answers
724 views

I believe this is a difference in Python 3.10 and above from older versions. Could someone explain this? import threading import time counter = 0 lock = threading.Lock() def increment(): global ...
RohanAlmighty'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
-1 votes
1 answer
475 views

I've been reading Fluent code by Luciano Ramalho and in the chapter 'Overview of Built-in Sequences' when describing C struct behind float he states: ".. That's why an array of floats is much ...
Andrey's user avatar
  • 338
0 votes
1 answer
652 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
2 answers
507 views

I know that Python maintains an internal storage of small-ish integers rather than creating them at runtime: id(5) 4304101544 When repeating this code after some time in the same kernel, the id is ...
Sebastian Wozny's user avatar
4 votes
1 answer
550 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
0 votes
1 answer
104 views

The following code: >>> class Foo: pass >>> class Spam(Foo()): pass will of course raise an error message: Traceback (most recent call last): File "<stdin>", line ...
yixuan's user avatar
  • 405
2 votes
1 answer
631 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
2 votes
1 answer
645 views

Consider the following code: main.py import asyncio import websockets async def echo(websocket): async for message in websocket: await websocket.send(message) print(message) async ...
OES's user avatar
  • 65
1 vote
1 answer
462 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
2 votes
1 answer
158 views

>>> a = 1 >>> b = 2 >>> id(a), id(b), id(b) - id(a) (1814458401008, 1814458401040, 32) Is the memory address returned by id in bits or in bytes? Per the docs: The current ...
BPDev's user avatar
  • 927
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
58 votes
2 answers
7k views

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 = ...
Kelly Bundy's user avatar
1 vote
1 answer
232 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
17 votes
1 answer
942 views

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, objects have no dictionary. ...
Jorge Luis's user avatar
1 vote
1 answer
99 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
33 votes
2 answers
5k views

My understanding of yield from is that it is similar to yielding every item from an iterable. Yet, I observe the different behavior in the following example. I have Class1 class Class1: def ...
erzya's user avatar
  • 628
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
0 votes
1 answer
115 views

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 *...
Amir reza Riahi's user avatar
13 votes
1 answer
908 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
2 votes
0 answers
121 views

I am studying the CPython's objects system implementation and I struggling to understand the differences between and purposes of the PyTypeObject and PyType_Type structs. At first sight, I thought ...
arthurr's user avatar
  • 21
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 vote
1 answer
657 views

I can't find the Python source code for __getattribute__. I looked in "object", and "type" classes, but I only see the function declaration. I don't see the actual definition.
user1187968's user avatar
  • 8,146

1
2 3 4 5
16