Reputation: 523
I am a beginner in programming, and trying to learn Python from "Learning Python by Mark Lutz". In the 'Introducing the Python Interpreter' chapter the author states the following:
Depending on which flavor of Python you run, the interpreter itself may be implemented as a C program, a set of Java classes, or something else.
I am unable to understand how a piece of Python code is distinguished for a division to be made in the interpreter (either C program or Java classes as the author states).
Upvotes: 1
Views: 2537
Reputation: 11
"how a piece of Python code is distinguished for a division to be made in the interpreter (either C program or Java classes as the author states)"
Below are some pointers that might answer your question :
Usability :
So to answer in short, its not your piece of code that will decide which interpreter but you the user based on your project architecture(whether it has java library/framework dependencies) decides whether to use CPython or Jython.
Note : For a project both CPython and Jython can be used together, but remember only one interpreter can use the run-time environment at a time.
Upvotes: 0
Reputation: 110261
A piece of Python code, is just Python code. What the author is saying is that there are different Python interpreters - all of which do run Python code . these different Python interpreters can be programs written in C, in Java, in C#, or in Python itself. They are the program that is run when you type python
at the command line, or pick python
from an application menu. Ordinarily it will be CPython: the reference implementation of the language made by the Python Software foundation, which is written in C.
All in all, this is definitely non-essential for learning Python - just skip ahead, and come back to this issue once you are more familiarized with the Language.
Upvotes: 8
Reputation: 46960
It sounds like your true question is how a Python interpreter does what it does. This is the subject of entire college-level courses, but here is the thrust.
The interpreter reads the Python source with a specialized function called a parser. This is an input processor that understands the exact structure of the Python language in the form of a context free grammar.
The parsing procedure checks the input for compliance with Python syntax rules (here is where syntax errors originate) and produces a data structure that has distilled the raw meaning of the Python program in the form of very simple steps represented in a simple array of bytes called byte code.
The complete process of input, parsing, and byte code production is called compilation. Yes, at its heart, Python has a simple kind of compiler.
Byte codes do very basic things like arithmetic, testing and jumping to other byte code locations based on test results, string operations, calling and returning from functions, input, output, and all the hundreds of fundamental operations that Python programs perform that, when combined and executed quickly, make programs seem powerful.
Where the original Python source was reasonably close to human language (well, sort of...), the byte code is reasonably close to machine language: the patterns of 1's and zeros that processors understand as instructions. Byte code is what's written to .pyc
files when a program runs.
It takes only a quite small and simple program (and hopefully a fast one) called the byte code interpreter (yes an interpreter within the Python interpreter) to process the byte code in order to have the processor do the work that the original Python program intended.
Of course if a .pyc
file already exists and is newer than the corresponding source code, the compilation can be skipped.
There you have it... A college course in a few sentences. I have skipped enormous amounts of detail, and (a caveat) some of what I've written applies only to some of the many Python implementations out there, but it's a start at understanding.
The author of your book is saying that the implementation language of all above might be C or Java (the Jython implementation of Python). He left out that only a minimal core of Python is implemented in C or Java. The rest is in Python itself.
If you're interested in knowing more, there are some reasonably good, low-cost and free sources. See for example this article and also for not too much money try Scheme 9 From Empty Space for how an interpreter of a language other than Python is implemented.
Upvotes: 4
Reputation: 3346
The reference interpreter of Python, the one that the others use as a guide, is written in C, and it is nicknamed CPython (not to be confused with Cython, which isn't an interpreter). It compiles Python code to Python bytecode and runs it on its virtual machine, which is also written in C.
There are other interpreters. Here are a few that are most well-known.
Theoretically and ideally, all strictly-legal Python code should run the same on any of these. Think of it like different compilers for C: strictly-legal C code will compile to equivalent programs on any of the compilers
void main()
in Visual Studio is non-standard).windows.h
library (like CPython won't have access to the Java libraries).Upvotes: 3