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
14
votes
4 answers

What resources does an instance of a class use?

How efficient is python (cpython I guess) when allocating resources for a newly created instance of a class? I have a situation where I will need to instantiate a node class millions of times to make a tree structure. Each of the node objects should…
Alex
  • 1,442
  • 2
  • 11
  • 16
14
votes
3 answers

Why does naive string concatenation become quadratic above a certain length?

Building a string through repeated string concatenation is an anti-pattern, but I'm still curious why its performance switches from linear to quadratic after string length exceeds approximately 10 ** 6: # this will take time linear in n with the…
max
  • 49,282
  • 56
  • 208
  • 355
14
votes
1 answer

'{0}'.format() is faster than str() and '{}'.format() using IPython %timeit and otherwise using pure Python

So it's a CPython thing, not quite sure that it has same behaviour with other implementations. But '{0}'.format() is faster than str() and '{}'.format(). I'm posting results from Python 3.5.2, but, I tried it with Python 2.7.12 and the trend is the…
vishes_shell
  • 22,409
  • 6
  • 71
  • 81
14
votes
1 answer

Why is copying a list using a slice[:] faster than using the obvious way?

Why is shallow-copying a list using a slice so much faster than using list builtin? In [1]: x = range(10) In [2]: timeit x_ = x[:] 10000000 loops, best of 3: 83.2 ns per loop In [3]: timeit x_ = list(x) 10000000 loops, best of 3: 147 ns per…
wim
  • 338,267
  • 99
  • 616
  • 750
14
votes
1 answer

What method does Python 2 use to print tuples?

Python's print statement normally seems to print the repr() of its input. Tuples don't appear to be an exception: >>> print (1, 2, 3) (1, 2, 3) >>> print repr((1, 2, 3)) (1, 2, 3) But then I stumbled across some strange behavior while messing…
ashastral
  • 2,818
  • 1
  • 21
  • 32
13
votes
1 answer

How are small sets stored in memory?

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) ... if size not in seen: ... seen[size] = len(s) ... …
wim
  • 338,267
  • 99
  • 616
  • 750
13
votes
1 answer

create golang bindings for a python module

I want to write golang bindings for an existing (third party) Python module. The purpose is that I want to use the API that the Python module provides in Golang. I already found golang bindings for Python's C API (go-python3 for py3 and go-python…
Chris
  • 567
  • 6
  • 24
13
votes
4 answers

Returning int from a __str__ method in Python

I know, that the purpose of str() method is to return the string representation of an object, so I wanted to test what happens if I force it to make something else. I've created a class and an object: class MyClass(object): def __str__(self,…
Narta
  • 131
  • 1
  • 5
13
votes
1 answer

What exactly is the optimization `functools.partial` is making?

CPython 3.6.4: from functools import partial def add(x, y, z, a): return x + y + z + a list_of_as = list(range(10000)) def max1(): return max(list_of_as , key=lambda a: add(10, 20, 30, a)) def max2(): return max(list_of_as ,…
pawelswiecki
  • 562
  • 1
  • 4
  • 14
13
votes
2 answers

What are the rules for cpython's string interning?

In python 3.5, is it possible to predict when we will get an interned string or when we will get a copy? After reading a few Stack Overflow answers on this issue I've found this one the most helpful but still not comprehensive. Than I looked at…
user5164080
13
votes
1 answer

Why does my Sieve of Eratosthenes work faster with integers than with booleans?

I wrote a simple Sieve of Eratosthenes, which uses a list of ones and turns them into zeros if not prime, like so: def eSieve(n): #Where m is fixed-length list of all integers up to n '''Creates a list of primes less than or equal to n''' m…
Moorsy
  • 143
  • 6
13
votes
1 answer

Different behavior in Python script and Python IDLE?

In the Python IDLE: >>> a=1.1 >>> b=1.1 >>> a is b False But when I put the code in a script and run it, I will get a different result: $cat t.py a=1.1 b=1.1 print a is b $python t.py True Why did this happen? I know that is compares the id of two…
WKPlus
  • 6,955
  • 2
  • 35
  • 53
13
votes
1 answer

String matching performance: gcc versus CPython

Whilst researching performance trade-offs between Python and C++, I've devised a small example, which mostly focusses on a dumb substring matching. Here is the relevant C++: using std::string; std::vector
RomanK
  • 1,258
  • 7
  • 18
13
votes
3 answers

Why do up and down arrow commands not work in the Python command line interpreter?

I am using a VT100 terminal emulator on Linux. In bash, up and down arrows scroll through the last commands executed; they work as expected. Previous (up arrow) and next (down arrow) commands are not interpreted in the Python command line…
octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
12
votes
0 answers

Will async-generators that don't have 'await' in its finally-block be immediately closed when there is no reference to it?

According to this blog, normal generators will be immediately closed when there is no reference to it. (CPython exclusive though). My question is "Does this apply to async-generators that don't have 'await' in its finally-block?" Motivation I'm an…
Nattōsai Mitō
  • 778
  • 4
  • 13