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

How is the s=s+c string concat optimization decided?

Short version: If s is a string, then s = s + 'c' might modify the string in place, while t = s + 'c' can't. But how does the operation s + 'c' know which scenario it's in? Long version: t = s + 'c' needs to create a separate string because the…
no comment
  • 6,381
  • 4
  • 12
  • 30
16
votes
6 answers

What are some strategies to write python code that works in CPython, Jython and IronPython

Having tries to target two of these environments at the same time I can safely say the if you have to use a database etc. you end up having to write unique code for that environment. Have you got a great way to handle this situation?
minty
  • 22,235
  • 40
  • 89
  • 106
16
votes
2 answers

Python hasattr vs getattr

I have been reading lately some tweets and the python documentation about hasattr and it says: hasattr(object, name) The arguments are an object and a string. The result is True if the string is the name of >> one of the object’s attributes, False…
raulcumplido
  • 405
  • 1
  • 3
  • 6
16
votes
1 answer

CPython - locking the GIL in the main thread

The documentation for CPython thread support is frustratingly contradictory and sparse. In general, it seems that everyone agrees that multi-threaded C applications which embed Python must always acquire the GIL before calling the Python…
Siler
  • 8,976
  • 11
  • 64
  • 124
16
votes
1 answer

Boost.python vs Cython for C++/python interface

I know this has been asked a thousand times, however i need to choose a library that can expose C++ functions and methods to python. Considering my application, which mainly is a scientific (matrix) library, and python generator's matureness,…
Carmellose
  • 4,815
  • 10
  • 38
  • 56
15
votes
2 answers

Modify *existing* variable in `locals()` or `frame.f_locals`

I have found some vaguely related questions to this question, but not any clean and specific solution for CPython. And I assume that a "valid" solution is interpreter specific. First the things I think I understand: locals() gives a non-modifiable…
MariusSiuram
  • 3,380
  • 1
  • 21
  • 40
15
votes
6 answers

ValueError: failed to parse CPython sys.version after using conda command

I'm running into an error that I can't solve despite others having reported the same error. I am connecting remotely to a Linux machine. I have installed the latest version of anaconda: $ bash Anaconda2-2.4.0-Linux-x86_64.sh // A lot of python…
ComputerScientist
  • 936
  • 4
  • 12
  • 20
15
votes
1 answer

How are variables names stored and mapped internally?

I read https://stackoverflow.com/a/19721096/1661745 and it seems that in CPython, variables are simply names that are associated with references. There are several things going on with the statement x=5: an int object with the value of 5 is…
onepiece
  • 3,279
  • 8
  • 44
  • 63
15
votes
2 answers

Possible to execute Python bytecode from a script?

Say I have a running CPython session, Is there a way to run the data (bytes) from a pyc file directly? (without having the data on-disk necessarily, and without having to write a temporary pyc file) Example script to show a simple use-case: if foo: …
ideasman42
  • 42,413
  • 44
  • 197
  • 320
15
votes
1 answer

How does module loading work in CPython?

How does module loading work in CPython under the hood? Especially, how does the dynamic loading of extensions written in C work? Where can I learn about this? I find the source code itself rather overwhelming. I can see that trusty ol' dlopen() and…
Praxeolitic
  • 22,455
  • 16
  • 75
  • 126
15
votes
1 answer

Why doesn't Python always require spaces around keywords?

Why can spaces sometimes be omitted before and after key words? For example, why is the expression 2if-1e1else 1 valid? Seems to work in both CPython 2.7 and 3.3: $ python2 Python 2.7.3 (default, Nov 12 2012, 09:50:25) [GCC 4.2.1 Compatible Apple…
jterrace
  • 64,866
  • 22
  • 157
  • 202
15
votes
1 answer

C Python: Running Python code within a context

The Python C API function PyEval_EvalCode let's you execute compiled Python code. I want to execute a block of Python code as if it were executing within the scope of a function, so that it has its own dictionary of local variables which don't…
Channel72
  • 24,139
  • 32
  • 108
  • 180
15
votes
2 answers

Python source code for built-in "in" operator

I am trying to find the implementation of the built-in in operator in the (C) Python source code. I have searched in the built-in functions source code, bltinmodule.c, but cannot find the implementation of this operator. Where can I find this…
Michael
  • 2,827
  • 4
  • 30
  • 47
14
votes
4 answers

Tuple slicing not returning a new object as opposed to list slicing

In Python (2 and 3). Whenever we use list slicing it returns a new object, e.g.: l1 = [1,2,3,4] print(id(l1)) l2 = l1[:] print(id(l2)) Output >>> 140344378384464 >>> 140344378387272 If the same thing is repeated with tuple, the same object is…
Vijay Jangir
  • 584
  • 3
  • 15
14
votes
3 answers

Why does setting a descriptor on a class overwrite the descriptor?

Simple repro: class VocalDescriptor(object): def __get__(self, obj, objtype): print('__get__, obj={}, objtype={}'.format(obj, objtype)) def __set__(self, obj, val): print('__set__') class B(object): v =…
Michael Carilli
  • 371
  • 2
  • 12