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
40
votes
1 answer

Why is Python 3 is considerably slower than Python 2?

I've been trying to understand why Python 3 is actually taking much time compared with Python 2 in certain situations, below are few cases I've verified from python 3.4 to python 2.7. Note: I've gone through some of the questions like Why is there…
gsb-eng
  • 1,211
  • 1
  • 9
  • 16
37
votes
1 answer

Why is list(x for x in a) faster for a=[0] than for a=[]?

I tested list(x for x in a) with three different CPython versions. On a = [0] it's significantly faster than on a = []: 3.9.0 64-bit 3.9.0 32-bit 3.7.8 64-bit a = [] a = [0] a = [] a = [0] a = [] a = [0] 465 ns 412 ns 543…
Kelly Bundy
  • 23,480
  • 7
  • 29
  • 65
37
votes
5 answers

How to load a C# dll in python?

how can I load a c# dll in python? Do I have to put some extra code in the c# files? (like export in c++ files) I don't want to use IronPython. I want to import a module to Python!
aF.
  • 64,980
  • 43
  • 135
  • 198
35
votes
3 answers

What is a "code object" mentioned in this TypeError message?

While trying to use Python's exec statement, I got the following error: TypeError: exec: arg 1 must be a string, file, or code object I don't want to pass in a string or a file, but what is a code object, and how do I create one?
oneself
  • 38,641
  • 34
  • 96
  • 120
35
votes
1 answer

What is co_names?

The description for co_names in the inspect module reads: tuple of names of local variables However in practice it appears that co_names is a tuple of global variable names while co_varnames is a tuple of local variable names (and argument names).…
Alex
  • 18,484
  • 8
  • 60
  • 80
33
votes
7 answers

Python or IronPython

How does IronPython stack up to the default Windows implementation of Python from python.org? If I am learning Python, will I be learning a subtley different language with IronPython, and what libraries would I be doing without? Are there,…
johnc
  • 39,385
  • 37
  • 101
  • 139
32
votes
3 answers

Is a variable swap guaranteed to be atomic in python?

With reference to the following link: http://docs.python.org/faq/library.html#what-kinds-of-global-value-mutation-are-thread-safe I wanted to know if the following: (x, y) = (y, x) will be guaranteed atomic in cPython. (x and y are both python…
dhruvbird
  • 6,061
  • 6
  • 34
  • 39
32
votes
2 answers

How is tuple implemented in CPython?

I've been trying to learn how CPython is implemented under the scenes. It's great that Python is high level, but I don't like treating it like a black box. With that in mind, how are tuples implemented? I've had a look at the source…
Alex L
  • 8,748
  • 5
  • 49
  • 75
30
votes
1 answer

Why is a False value (0) smaller in bytes than True (1)?

I was playing around with sys's getsizeof() and found that False (or 0) consists of less bytes than True (or 1). Why is that? import sys print("Zero: " + str(sys.getsizeof(0))) print("One: " + str(sys.getsizeof(1))) print("False: " +…
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
30
votes
2 answers

Why does id({}) == id({}) and id([]) == id([]) in CPython?

Why does CPython (no clue about other Python implementations) have the following behavior? tuple1 = () tuple2 = () dict1 = {} dict2 = {} list1 =…
spenthil
  • 3,373
  • 29
  • 17
30
votes
3 answers

can you recover from reassigning __builtins__ in python?

If I open up interactive mode and type: __builtins__ = 0 # breaks everything have I completely broken the session? If so, what is going on behind the scenes to assign __builtins__ to the builtin module that can't be handled by the interpreter? If…
Hart Simha
  • 862
  • 8
  • 14
30
votes
2 answers

Why does str.split not take keyword arguments?

I came across this - in my view - strange behaviour: "a b c".split(maxsplit=1) TypeError: split() takes no keyword arguments Why does str.split() not take keyword arguments, even though it would make sense? I found this behavior both in Python2 and…
Peter Smit
  • 27,696
  • 33
  • 111
  • 170
28
votes
2 answers

Comparing None with built-in types using arithmetic operators?

Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> None > 0 False >>> None == 0 False >>> None < 0 True Is comparing None using arithmetic…
Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
28
votes
3 answers

Clarification for "it should be possible to change the value of 1" from the CPython documentation

See this link: https://docs.python.org/3/c-api/long.html#c.PyLong_FromLong The current implementation keeps an array of integer objects for all integers between -5 and 256; when you create an int in that range, you actually just get back a…
user13595466
28
votes
2 answers

Why is deque implemented as a linked list instead of a circular array?

CPython deque is implemented as a doubly-linked list of 64-item sized "blocks" (arrays). The blocks are all full, except for the ones at either end of the linked list. IIUC, the blocks are freed when a pop / popleft removes the last item in the…
max
  • 49,282
  • 56
  • 208
  • 355
1 2
3
90 91