Search code examples
pythonciostdoutstdin

How the python interpreter receives and outputs data?


What happens when I write print('Something')? Does the interpreter work directly with the system I / O means, or does it use some kind of system API, which gives information that something needs to be displayed in the console?

Likewise for input. If I call the input() function, then it is the python interpreter itself that will read the input values, or it will again delegate this work to some system API, which will wrap the input.

I know that python is written in C language (at least its main implementation). It turns out that it is the C language inside the interpreter that is responsible for the input / output?

All my attempts to find an answer come down to articles where they tell me that in input() you need to press a button from the keyboard, and the print() function will print something in the console. I already know this, I want to know what is happening at the level below.


Solution

  • When you write print('Something'), here's what happens at a lower level:

    1. Python Interpreter: The Python interpreter is responsible for executing your code line by line.
    2. print() Function: When the interpreter encounters the print('Something') statement, it first evaluates the expression within the parentheses, which is just a string in this case, 'Something'.
    3. System Call: The Python interpreter uses a system call to communicate with the operating system or the environment it is running in (e.g., a console or terminal). The system call is an interface provided by the operating system to access various low-level functions, including input/output operations.
    4. System API: The system call is handled by a system API. This API is part of the operating system and allows the Python interpreter to interact with the underlying I/O facilities of the system. It provides a way for the Python interpreter to communicate with the console or terminal and display the text 'Something'.
    5. Display: Finally, the text 'Something' is displayed in the console or terminal where you are running the Python program.

    Similarly, when you use input():

    1. Python Interpreter: The Python interpreter is executing your code.
    2. input() Function: When the interpreter encounters input(), it pauses the program's execution and waits for the user to input some text.
    3. System API: The interpreter uses a system API to access the underlying I/O facilities of the system and wait for the user to provide input.
    4. User Input: The system API captures the user's input from the console or terminal and passes it back to the Python interpreter.
    5. Continue Execution: The Python interpreter resumes execution with the input provided by the user, and you can assign it to a variable or use it in your program as needed.

    So, in summary, when you interact with the Python interpreter using functions like print() and input(), the interpreter itself relies on system APIs provided by the operating system to handle the actual input and output operations. This abstraction allows Python to work seamlessly across different platforms and environments.