I came across this word "implementation". CPython is one of the most common implementations of Python. What exactly is an implementation?

I researched a bit on how a Python code runs. First, it is compiled and converted into bytecode and then, using PVM or interpreter (not sure), it is converted into machine language which is then executed by CPU.

In all of these procedures, where is CPython (or Jython, PyPy, etc.)?

Are PVM and interpreter the same?

Secondly, I read somewhere that PVM is written in C. Is it true? Is that what it means when they say the implementation is in C?

  • Sry ,rectified that Apr 9 '19 at 4:53
  • Just to provide some context: in short, computers are dumb. They don't know about programming languages and other higher level concepts we use, they just know how to execute a set of simple instructions. Compilers (some of them, at least, aided by linkers and loaders, the operating system, and hardware) translate high level languages into machine code, but in the process they also generate a bunch of extra code and data structures, and provide certain logic, all to support concepts in high level languages, like objects, inheritance and virtual method calls, procedure calls, even variables. Apr 9 '19 at 11:04
  • There can be a number of different elements, layers and variations to this (runtimes, intermediate languages, interpreters, JIT compilers, virtual machines), but it all has to come down to machine instructions eventually - so in that sense, a language has to be implemented by some other code that will make it possible for programs written in it to execute. Apr 9 '19 at 11:04
  • Here is CPython: github.com/python/cpython
    – user214290
    Apr 9 '19 at 11:30

Think about how a human language works. In theory, there's some kind of document(s) that lays down what the rules are for what constitutes "English". There's a set of definitions for words, rules for how grammar works to assemble those words into meaningful sentences, and so forth. Each of us communicates in English using our own ideas of what those words mean and how English grammar works.

So we can analogize this to programming languages. You have a language like Python. The "people" who "speak" Python in this analogy are not programmers; the "speakers" of Python are what we call "implementations". These are the tools which understand Python and make the computer actually do what the Python instructions say to do.

CPython is one such "speaker" of Python. Once, it was the only Python implementation. CPython is a specific implementation that is largely written in C. Jython is an implementation of Python written in Java. They both effectively do the same thing, understanding the same grammar for the same purpose. But they do it in different ways.

Note that in the first paragraph, I only said that "in theory" languages have some document defining what they mean. That's because there are many human languages that don't have formal rules. And even within formal rules, there will always be dialects, neologisms, and other things as languages evolve naturally. Note that this makes it possible for one person who speaks one language to fail to understand someone who ostensibly speaks the same language.

This happens because the two people, the two "implementations," disagree about what the language actually is.

Programming languages tend to take one of two routes with regard to definition, or "standardization". They can have a de-facto standard or a de-jure standard. Indeed, the word "implementation" in this context means to "implement" a "standard".

Languages with a de-jure standard have a hard document(s) that lays down everything about what the language is. Hopefully, it formally specifies everything about the language, detailing in depth the behavior of every syntactic construct. If two implementations differ in behavior when given the same code, then one of the following is happening:

  1. One or both of them is not implementing the standard correctly.
  2. The standard is poorly specified for that particular piece of language. That is, it doesn't say what the behavior ought to be, or is confusingly worded such that it is ambiguous as to what should actually happen.
  3. The standard explicitly says that the behavior of the code is either defined by the implementation or completely undefined (C++ loves saying this).

Note that there is a huge difference between a formal specification for a language and reference documentation.

A de-facto standard is where you define the language by picking one implementation and saying "whatever that thing says is language X is language X". This means that if two implementations differ, the de-facto standard one is what is correct.

This also means that if the de-facto standard has quirks in it, every implementation of that language must reproduce those quirks. For example, if the de-facto standard has a hard limit of only 32 function parameters for some reason, then implementations of that language which allow more function parameters are technically wrong.

If the de-facto standard has (non-crashing) bugs in it... well, that's the thing: it can't have bugs. "Bug" is defined relative to the standard. And the de-facto standard implementation is the standard; "bugs" in it are therefore features, until the next version comes out that changes things. And such things are, on some level, changes to language features, not bug fixes.

Many languages start with de-facto standards and evolve towards de-jure standards. Both C# and Java started without formal language specifications, but both of them have them now. C++ was an ill-defined mish-mash of stuff until C++98 formalized what it meant to be C++.

Python is kind of half-and-half at this point. It has a language reference document that proports to define the language, but it isn't a "formal specification". From the language reference:

I chose to use English rather than formal specifications for everything except syntax and lexical analysis. This should make the document more understandable to the average reader, but will leave room for ambiguities. Consequently, if you were coming from Mars and tried to re-implement Python from this document alone, you might have to guess things and in fact you would probably end up implementing quite a different language.

So in the case of ambiguity, you generally would defer to CPython's behavior. Then again, there are plenty of formal specifications that have ambiguous areas as well.

  • Wow. Nice crystal-ball reading of the question. Unfortunately, I left my crystal ball at home yesterday when I answered the question. Apr 9 '19 at 15:07
  • Thanks for the answer. I understood, implementation is the program that makes the python code run. But , can you please let me know , in the following process of conversion where exactly is cpython in work (python code -> compiler -> Bytecode -> PVM(interpreter) -> Machine code) Apr 11 '19 at 5:29
  • 4
    @AkshayNarwadkar: The Python implementation, whichever implementation that may be, is everything between "python code" and "machine code". All of that. It's the stuff that makes a Python program do what Python programs are supposed to do. Apr 11 '19 at 5:41
  • okay , thanks ,but it led my to me another doubt ,AS the PVM(interpreter) is written in "C", but if the implementation is not Cpython , does the PVM still continue to be in "C" is not an issue or if the implementation is jython , it has any effect in its communication with PVM? and then with machine code ? Apr 13 '19 at 7:18
  • 3
    @AkshayNarwadkar: They are not separate pieces that get mixed and matched between implementations. Each implementation provides all of those things. CPython contains a Python Virtual Machine. Jython contains a Python Virtual Machine (or rather, a Python environment running on the Java Virtual Machine, but whatever). They don't "communicate" with it any more than you "communicate" with your left arm. Apr 13 '19 at 13:44

Roughly speaking, "Python" is a concept. It is an idea for a programming language. Of course, it's more than just an idea, it's a fully fleshed out specification of a programming language. However, until there is an actual program that can read a .py file and execute the code, it's just a concept.

For a .py file to be useful, it must be processed by a program that understands the language and is able to convert it to the language of the underlying machine.

When someone or some team writes a program that can process a file written in the python language (eg: python.exe, /usr/local/bin/python, etc), that program becomes an implementation of the language.

These programs that process .py files can be written in many languages. For example, ironpython is a .net program that can process a .py file. Jython is a java program that can process a .py file. CPython is a C-based program that can process a .py file. Each of those are an implementation of python.

  • Thanks for the answer. I understood, implementation is the program that makes the python code run. But , can you please let me know , in the following process of conversion where exactly is cpython in work (python code -> compiler -> Bytecode -> PVM(interpreter) -> Machine code) Apr 11 '19 at 5:30
  • 1
    @AkshayNarwadkar - cpython is all of this: compiler -> Bytecode -> PVM(interpreter) -> Machine code. Apr 11 '19 at 13:01
  • ok thanks , but As you said, we need a program to process the python and that program is the implementation. So shall i not learn python but cpython (or for that matter any implementation ) as cpython is going to help me with executing the python code ? Apr 13 '19 at 7:02
  • Also , ,AS the PVM(interpreter) is written in "C", but if the implementation is not Cpython , does the PVM still continue to be in "C" is not an issue or if the implementation is jython , it has any effect in its communication with PVM? and then with machine code ? Apr 13 '19 at 7:20

I am not familiar with Python, but I suspect that saying CPython is an implementation of Python is the same as saying GCC is an implementation of ANSI Standard C and LAM MPI, MPICH, OpenMPI are implementations of the MPI specification. A programming language is specified by some standard-setting body and "implemented" by compilers or interpreters (or hybrid). C is standardized by ANSI, which is not responsible for providing any compiler. Vendors such as Microsoft or volunteers such as the open source community develop compilers. And commonly a compiler accepts more than the standard does, i.e. extends the standard (by offering additional features), or "clarifies" the standard, i.e., defines what the standard does not define. In effect, the compiler defines a variant or "dialect" of the language. As to CPython, I suspect that that particular compiler/interpreter is written in C, so it is called CPython. Also see this article.

Not the answer you're looking for? Browse other questions tagged or ask your own question.