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
8
votes
2 answers

Is it true that in multiprocessing, each process gets it's own GIL in CPython? How different is that from creating new runtimes?

Are there any caveats to it? I have a few questions related to it. How costly is it to create more GILs? Is it any different from creating a separate python runtime? Once a new GIL is created, will it create everything (objects, variables, stack,…
sprksh
  • 2,204
  • 2
  • 26
  • 43
8
votes
3 answers

When CPython set `in` operator is O(n)?

I was reading about the time complexity of set operations in CPython and learned that the in operator for sets has the average time complexity of O(1) and worst case time complexity of O(n). I also learned that the worst case wouldn't occur in…
ruohola
  • 21,987
  • 6
  • 62
  • 97
8
votes
4 answers

How can you programmatically tell the CPython interpreter to enter interactive mode when done?

If you invoke the cpython interpreter with the -i option, it will enter the interactive mode upon completing any commands or scripts it has been given to run. Is there a way, within a program to get the interpreter to do this even when it has not…
Nick
  • 21,555
  • 18
  • 47
  • 50
8
votes
1 answer

How come CPython is faster than PyPy on the two tests "slowspitfire" and "waf"?

Judging from the benchmarks posted on the PyPy Speed Center it appears as if PyPy is faster than CPython for all but two of the tests presented. CPython is faster than PyPy on the two tests "slowspitfire" and "waf". Why is that? What kind of…
knorv
  • 49,059
  • 74
  • 210
  • 294
8
votes
2 answers

Docs for the internals of CPython Implementation

I am currently in the process of making an embedded system port of the CPython 3.0 Python interpreter and I'm particularly interested in any references or documentation that provides details about the design and structure of code for Release 3.0 or…
Tall Jeff
  • 9,834
  • 7
  • 44
  • 61
8
votes
2 answers

Why does the `is` operator behave differently in a script vs the REPL?

In python, two codes have different results: a = 300 b = 300 print (a==b) print (a is b) ## print True print ("id(a) = %d, id(b) = %d"%(id(a), id(b))) ## They have same address But in shell mode(interactive mode): >>> a = 300 >>> b = 300 >>> a…
Bokyun Na
  • 79
  • 2
8
votes
2 answers

Python string concatenation internal details

Assume that we have a list of strings and we want to create a string by concatenating all element in this list. Something like this: def foo(str_lst): result = '' for element in str_lst: result += element return result Since…
Seljuk Gulcan
  • 1,826
  • 13
  • 24
8
votes
1 answer

Python Source Code - Update Grammar

I am studying a little about the source code of Python and I decided to put into practice some changes in the grammar, so I downloaded the source code of version 3.7. I am following the guidelines of PEP…
Jonny
  • 408
  • 4
  • 12
8
votes
1 answer

When to Py_INCREF?

I'm working on a C extension and am at the point where I want to track down memory leaks. From reading Python's documentation it's hard to understand when to increment / decrement reference count of Python objects. Also, after couple days spending…
wvxvw
  • 8,089
  • 10
  • 32
  • 61
8
votes
2 answers

Is there an implementation of _rational_ interval arithmetic in Python?

Is there an implementation of rational interval arithmetic in Python? This uses floats, not rationals. If not, is there any implementation of rationals in Python that includes ±∞ ?
user380772
8
votes
2 answers

Why is 'new_file += line + string' so much faster than 'new_file = new_file + line + string'?

Our code takes 10 minutes to siphon thru 68,000 records when we use: new_file = new_file + line + string However when we do the following it takes just 1 second: new_file += line + string Here is the code: for line in content: import time import…
gunslingor
  • 1,358
  • 12
  • 34
8
votes
2 answers

Why single python process's cpu usage can be more than 100 percent?

Because of GIL, I thought a multi-thread python process can only have one thread running at one time, thus the cpu usage can not be more than 100 percent. But I found the code bellow can occupy 950% cpu usage in top. import threading import…
WKPlus
  • 6,955
  • 2
  • 35
  • 53
8
votes
2 answers

Tuple declaration in Python

In python, one can declare a tuple explicitly with parenthesis as such: >>> x = (0.25, 0.25, 0.25, 0.25) >>> x (0.25, 0.25, 0.25, 0.25) >>> type(x) Alternatively, without parenthesis, python automatically packs its into a immutable…
alvas
  • 115,346
  • 109
  • 446
  • 738
8
votes
2 answers

Does cPython use multiple cores for built-in functions such as sort, any, all?

I understand that cPython has a GIL so that your script can't run on multiple cores without using the multiprocessing module. But is there anything to stop the built in functions such as sorting using multiple cores? I don't understand cPython…
eggbert
  • 3,105
  • 5
  • 30
  • 39
8
votes
3 answers

How do programming languages call code written in another language?

Sorry if this is too vague. I was recently reading about python's list.sort() method and read that it was written in C for performance reasons. I'm assuming that the python code just passes a list to the C code and the C code passes a list back, but…
Igglyboo
  • 837
  • 8
  • 21