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

How can I tell which python implementation I'm using?

Python has a few different implementations: CPython, Jython, PyPy, etc. I want to programmatically determine which implementation my code is running on. How can I do that? To be specific, write a function called get_implementation_name() for…
Stuart Berg
  • 17,026
  • 12
  • 67
  • 99
76
votes
4 answers

Different object size of True and False in Python 3

Experimenting with magic methods (__sizeof__ in particular) on different Python objects I stumbled over the following behaviour: Python 2.7 >>> False.__sizeof__() 24 >>> True.__sizeof__() 24 Python 3.x >>> False.__sizeof__() 24 >>>…
Simon Fromme
  • 3,104
  • 18
  • 30
67
votes
3 answers

OrderedDict comprehensions

Can I extend syntax in python for dict comprehensions for other dicts, like the OrderedDict in collections module or my own types which inherit from dict? Just rebinding the dict name obviously doesn't work, the {key: value} comprehension syntax…
wim
  • 338,267
  • 99
  • 616
  • 750
67
votes
4 answers

How exactly is Python Bytecode Run in CPython?

I am trying to understand how Python works (because I use it all the time!). To my understanding, when you run something like python script.py, the script is converted to bytecode and then the interpreter/VM/CPython–really just a C Program–reads in…
mergesort
  • 5,087
  • 13
  • 38
  • 63
65
votes
3 answers

deque.popleft() and list.pop(0). Is there performance difference?

deque.popleft() and list.pop(0) seem to return the same result. Is there any performance difference between them and why?
Bin
  • 3,645
  • 10
  • 33
  • 57
59
votes
2 answers

Why don't Python sets preserve insertion order?

I was surprised to discover recently that while dicts are guaranteed to preserve insertion order in Python 3.7+, sets are not: >>> d = {'a': 1, 'b': 2, 'c': 3} >>> d {'a': 1, 'b': 2, 'c': 3} >>> d['d'] = 4 >>> d {'a': 1, 'b': 2, 'c': 3, 'd': 4} >>>…
Bart Robinson
  • 1,024
  • 1
  • 9
  • 10
58
votes
2 answers

Why is string's startswith slower than in?

Surprisingly, I find startswith is slower than in: In [10]: s="ABCD"*10 In [11]: %timeit s.startswith("XYZ") 1000000 loops, best of 3: 307 ns per loop In [12]: %timeit "XYZ" in s 10000000 loops, best of 3: 81.7 ns per loop As we all know, the in…
WKPlus
  • 6,955
  • 2
  • 35
  • 53
57
votes
2 answers

Why is b.pop(0) over 200 times slower than del b[0] for bytearray?

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 = bytearray(1000000)') print(t1 / t2) Time…
Kelly Bundy
  • 23,480
  • 7
  • 29
  • 65
51
votes
2 answers

Why is the size of 2⁶³ 36 bytes, but 2⁶³-1 is only 24 bytes?

Everything in Python is an object. So the size of an int in Python will be larger than usual. >>> sys.getsizeof(int()) 24 OK, but why does it take 12 more bytes for 2⁶³ compared too 2⁶³ - 1 and not just one? >>> sys.getsizeof(2**63) 36 >>>…
T.Nel
  • 1,540
  • 2
  • 18
  • 34
49
votes
2 answers

Why is string comparison so fast in python?

I became curious to understand the internals of how string comparison works in python when I was solving the following example algorithm problem: Given two strings, return the length of the longest common prefix Solution 1: charByChar My intuition…
david_adler
  • 9,690
  • 6
  • 57
  • 97
48
votes
5 answers

Print to standard printer from Python?

Is there a reasonably standard and cross platform way to print text (or even PS/PDF) to the system defined printer? Assuming CPython here, not something clever like using Jython and the Java printing API.
Prof. Falken
  • 24,226
  • 19
  • 100
  • 173
46
votes
2 answers

Are sets ordered like dicts in python3.6

Due to changes in dict implementation in Python 3.6 it is now ordered by default. Do sets preserve order as well now? I could not find any information about it but as both of those data structures are very similar in the way they work under the hood…
Quba
  • 4,776
  • 7
  • 34
  • 60
46
votes
3 answers

How does Python's Garbage Collector Detect Circular References?

I'm trying to understand how Python's garbage collector detects circular references. When I look at the documentation, all I see is a statement that circular references are detected, except when the objects involved have a __del__ method. If this…
user1245262
  • 6,968
  • 8
  • 50
  • 77
46
votes
1 answer

Why do -1 and -2 both hash to -2 in CPython?

Possible Duplicate: When is a python object's hash computed and why is the hash of -1 different? Why do -1 and -2 both hash to the same number if Python? Since they do, how does Python tell these two numbers apart? >>> -1 is -2 False >>> hash(-1)…
Peter
  • 469
  • 4
  • 5
42
votes
1 answer

Python string 'in' operator implementation algorithm and time complexity

I am thinking of how the in operator implement, for instance >>> s1 = 'abcdef' >>> s2 = 'bcd' >>> s2 in s1 True In CPython, which algorithm is used to implement the string match, and what is the time complexity? Is there any official document or…
mitchelllc
  • 1,607
  • 4
  • 20
  • 24
1
2
3
90 91