python-internals Questions

2

Solved

The following function returns None: In [5]: def f(): ...: pass So I was not surprised by this output: In [8]: dis.dis(f) 2 0 LOAD_CONST 0 (None) 3 RETURN_VALUE In [10]: f.__code__.co_cons...
Paryavi asked 27/12, 2014 at 13:33

1

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 pa...
Trichinopoly asked 17/11, 2023 at 16:19

1

Solved

>>> import sys >>> del sys.modules['sys'] >>> import sys >>> sys.modules Traceback (most recent call last): File "<stdin>", line 1, in <modul...
Qadi asked 3/5, 2024 at 19:5

2

Solved

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 N...

3

Solved

What algorithm is the built in sort() method in Python using? Is it possible to have a look at the code for that method?
Inclose asked 4/10, 2009 at 20:48

2

Solved

I am curious to understand how Python for loops work under the hood. I tried to implement it somewhat like the following code snippet, is that how the for loop has been implemented? my_list = [1, ...
Gilbertogilbertson asked 27/1, 2019 at 12:0

5

Solved

This is not a duplicate of this, I'll explain here. Consider x = 1.2. I'd like to separate it out into 1 and 0.2. I've tried all these methods as outlined in the linked question: In [370]: x = 1....
Savarin asked 19/7, 2017 at 15:10

7

We all know that eval is dangerous, even if you hide dangerous functions, because you can use Python's introspection features to dig down into things and re-extract them. For example, even if you d...
Attrahent asked 4/3, 2016 at 19:55

1

Solved

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: if not ...
Kilkenny asked 10/12, 2023 at 13:17

3

Solved

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 &...
Gentille asked 16/11, 2023 at 12:53

4

Solved

I have a little helper class: class AnyOf(object): def __init__(self, *args): self.elements = args def __eq__(self, other): return other in self.elements This lets me do sweet magic like: &...
Kenzie asked 30/11, 2017 at 15:51

2

Solved

>>> a=1 >>> b=1 >>> id(a) 140472563599848 >>> id(b) 140472563599848 >>> x=() >>> y=() >>> id(x) 4298207312 >>> id(y) 42...
Vtol asked 13/12, 2014 at 14:6

9

Solved

How do you access other class variables from a list comprehension within the class definition? The following works in Python 2 but fails in Python 3: class Foo: x = 5 y = [x for i in range(1)] P...

2

Solved

Here is my code: from memory_profiler import profile @profile def mess_with_memory(): huge_list = range(20000000) del huge_list print "why this kolaveri di?" This is what the output is, when...
Subrogate asked 10/1, 2014 at 20:0

1

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...
Cholecyst asked 5/4, 2023 at 15:5

1

Solved

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 task...
Microcurie asked 12/2, 2023 at 5:27

1

Solved

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 dictionar...
Olathe asked 12/1, 2023 at 8:38

5

Solved

Just came across this awesome __length_hint__() method for iterators from PEP 424 (https://www.python.org/dev/peps/pep-0424/). Wow! A way to get the iterator length without exhausting the iterator....
Goober asked 14/7, 2016 at 22:41

2

Solved

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 __init...
Imogen asked 26/12, 2022 at 16:48

9

Solved

Is it a linked list, an array? I searched around and only found people guessing. My C knowledge isn't good enough to look at the source code.
Closestool asked 12/10, 2010 at 17:56

22

Solved

I can't really think of any reason why Python needs the del keyword (and most languages seem to not have a similar keyword). For instance, rather than deleting a variable, one could just assign Non...
Alleneallentown asked 27/5, 2011 at 1:37

1

Solved

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)...
Nighttime asked 28/11, 2022 at 22:8

6

Solved

Dictionaries are insertion ordered as of Python 3.6. It is described as a CPython implementation detail rather than a language feature. The documentation states: dict() now uses a “compact” repres...
Pinguid asked 11/10, 2016 at 14:59

5

Solved

Two integers in Python have the same id: a = 10 b = 10 a is b >>> True If I take two lists: a = [1, 2, 3] b = [1, 2, 3] a is b >>> False according to this link Senderle answered...
Lenticel asked 4/7, 2016 at 17:21

6

This question is motivated by my another question: How to await in cdef? There are tons of articles and blog posts on the web about asyncio, but they are all very superficial. I couldn't find any ...
Huckaby asked 27/2, 2018 at 9:48

© 2022 - 2025 — McMap. All rights reserved.