Questions tagged [cpython]

The reference implementation of the Python programming language. Use this tag for questions specific to this implementation, general Python questions should just be tagged with "python".

CPython is the default and most widely used implementation of the programming language. It is written in C.

In addition to CPython, there are several other production-quality Python implementations: , a JIT-compiler. , written in Java and , which is written for the Common Language Runtime. There are also several experimental implementations.

CPython is a bytecode interpreter. It has a foreign function interface with several languages including C, in which one must explicitly write bindings in a language other than Python.

See also

1358 questions
26
votes
1 answer

Argument Unpacking wastes Stack Frames

When a function is called by unpacking arguments, it seems to increase the recursion depth twice. I would like to know why this happens. Normally: depth = 0 def f(): global depth depth += 1 f() try: f() except RuntimeError: …
Tesseract
  • 526
  • 3
  • 20
26
votes
2 answers

Why are these two functions different?

Take a look at this: >>> def f(): ... return (2+3)*4 ... >>> dis(f) 2 0 LOAD_CONST 5 (20) 3 RETURN_VALUE Evidently, the compiler has pre-evaluated (2+3)*4, which makes sense. Now, if I simply change…
arshajii
  • 127,459
  • 24
  • 238
  • 287
25
votes
3 answers

What happens behind the scenes when python adds small ints?

I was fiddling around with id recently and realized that (c?)Python does something quite sensible: it ensures that small ints always have the same id. >>> a, b, c, d, e = 1, 2, 3, 4, 5 >>> f, g, h, i, j = 1, 2, 3, 4, 5 >>> [id(x) == id(y) for x, y…
jsau
  • 837
  • 7
  • 11
24
votes
6 answers

Migrating from CPython to Jython

I'm considering moving my code (around 30K LOC) from CPython to Jython, so that I could have better integration with my java code. Is there a checklist or a guide I should look at, to help my with the migration? Does anyone have experience with…
itsadok
  • 28,822
  • 30
  • 126
  • 171
24
votes
1 answer

Nested list comprehension scope

The best way to explain my question is with an example: example.py: class A(object): integers = [1, 2, 3] singles = [i for i in integers] class B(object): integers = [1, 2, 3] pairs = [(i, j) for i in integers for j in…
sn6uv
  • 599
  • 5
  • 19
22
votes
1 answer

Docstrings in C extensions to Python?

When creating a C extension to Python, is it possible to be able to somehow write comments that are exposed as docstrings to users of the extension?
Tim McNamara
  • 18,019
  • 4
  • 52
  • 83
20
votes
2 answers

Why does Python optimize out "if 0", but not "if None"?

Why is it that if you compile a conditional expression like def f(): if None: print(222) if 0: print(333) the branches that use numbers get optimized out, but those that use None don't? Example: 3 0 LOAD_CONST …
user541686
  • 205,094
  • 128
  • 528
  • 886
20
votes
2 answers

What is Python's sequence protocol?

Python does a lot with magic methods and most of these are part of some protocol. I am familiar with the "iterator protocol" and the "number protocol" but recently stumbled over the term "sequence protocol". But even after some research I'm not…
MSeifert
  • 145,886
  • 38
  • 333
  • 352
19
votes
9 answers

How to generate a repeatable random number sequence?

I would like a function that can generate a pseudo-random sequence of values, but for that sequence to be repeatable every run. The data I want has to be reasonably well randomly distributed over a given range, it doesn't have to be perfect. I want…
Joe
  • 46,419
  • 33
  • 155
  • 245
19
votes
3 answers

concurrent.futures.ThreadPoolExecutor swallowing exceptions (Python 3.6)

I'm trying to use ThreadPoolExecutor in Python 3.6 on Windows 7 and it seems that the exceptions are silently ignored or stop program execution. Example code: #!/usr/bin/env python3 from time import sleep from concurrent.futures import…
18
votes
2 answers

Which characters are considered whitespace by split()?

I am porting some Python 2 code that calls split() on strings, so I need to know its exact behavior. The documentation states that when you do not specify the sep argument, "runs of consecutive whitespace are regarded as a single…
Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
18
votes
2 answers

CPython - Internally, what is stored on the stack and heap?

In C#, Value Types (eg: int, float, etc) are stored on the stack. Method parameters may also be stored on the stack as well. Most everything else, however, is stored on the heap. This includes Lists, objects, etc. I was wondering, does CPython do…
Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
18
votes
1 answer

CPython memory allocation

This is a post inspired from this comment about how memory is allocated for objects in CPython. Originally, this was in the context of creating a list and appending to it in a for loop vis a vis a list comprehension. So here are my questions: how…
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
17
votes
2 answers

Performance of map vs starmap?

I was trying to make a pure-python (without external dependencies) element-wise comparison of two sequences. My first solution was: list(map(operator.eq, seq1, seq2)) Then I found starmap function from itertools, which seemed pretty similar to me.…
godaygo
  • 2,215
  • 2
  • 16
  • 33
17
votes
5 answers

Using NumPy and Cpython with Jython

I must use a commercial Java library, and would like to do it from Python. Jython is robust and I am fine with it being a few dot releases behind. However, I would like to use NumPy as well, which obviously does not work with Jython. Options like…
gappy
  • 10,095
  • 14
  • 54
  • 73