Is my understanding of how Python is written / implemented correctly?

I want to understand how Python works at a basic level, and this, I hope, will help me understand a little more about the inner workings of other compiled / interpreted languages. Unfortunately, the compiler class is not much right now. From what I read on this site and elsewhere, people responding to “What is the base language written in Python written in” say that there is a difference between talking about the “rules” of a language compared to how the rules of the language used for use. So, is it right to say that Python (and other high-level languages) are just simple rules that are "written" in any natural language? And then the question of how they are used (when using tools compiled / interpreted to actually create things) may differ, and different languages ​​are used to implement compilers? Thus, in this case, CPython, IronPython, and Jython will be syntactically equal languages, all of which follow the same set of rules, only if these rules are implemented in their own languages.

Please let me know if I understand this correctly, if you have something to add that will further strengthen my understanding, or if I am mistakenly mistaken.

+5
source share
3 answers

Code written in Python should work on any Python interpreter. Python is essentially a specification of a programming language with a reference implementation (CPython). Whenever the specifications of Python and PEP are ambiguous, other interpreters usually prefer to implement the same behavior if they have no reason.

Thus, it is entirely possible that a program written in Python will behave differently in different implementations. This is because many programmers start "undefined behavior". For example, CPython has a “Global Interpreter Lock”, which means that only one thread is currently executing (modulo some conditions), but other interpreters do not have this behavior. So, for example, there is a different behavior regarding atomicity (for example, each bytecode instruction is atomic in CPython) as other interpreters.

You can think of it as C. C is a language specification, but there are many compilers that implement it: GCC, LLVM, Borland, MSVC ++, ICC, etc. There are programming languages ​​and implementations of these programming languages.

+7
source

You are right when you make a distinction between what language means and how it does what it means.

What does this mean

The first step to compiling a language is to analyze its code to generate an abstract syntax tree. This is a tree that defines what the code you wrote means what it should do. For example, if you have the following code

a = 1 if a: print('not zero') 

It will generate a tree that looks more or less like that.

  code ___________|______ | | declaration if __|__ ___|____ | | | | a 1 a print | 'not zero' 

This means that the code means, but does not report anything about how it executes it.

Edit: Of course, this is far from what Python parsons actually generate, I made a lot of simplification with the goal of readability. Fortunately for us, if you are interested in what is actually created, you can import ast , which provides a Python parser.

 import ast code = """ a = 1 if a: print('not zero') """ my_ast = ast.parse(code) 

Enjoy checking my_ast .

What is he doing

Once you have AST, you can convert it back to whatever you want. It can be C, it can be machine code, you can even convert it back to Python if you want. The most used Python implementation is CPython, which is written in C.

What happens under the hood is so close to your understanding. Firstly, a language is a set of rules that determine behavior, and only then is there an implementation for these languages ​​that determines how it does it. And yes, of course, you can have different implementations of the same language with slight differences in behavior.

+4
source

Basically, this is a bunch of dictionary data structures that implement functions, modules, etc. Global variables and their values ​​live in the dictionary for each module. Variables inside a class are another dictionary. Those inside the object are another dictionary, as well as those inside the function. Even a function call has its own dictionary, so different calls have different copies of local variables.

Unlike most other languages, it does not have lexical coverage, and, in my opinion, it was designed to implement 1 encoder using dictionaries as easily as possible.

-2
source

Source: https://habr.com/ru/post/1274746/


All Articles