Python Interpreter
Python interpreter is a translator.
➢ Python compiler generates bytecode for the interpreter.
➢ The python interpreter uses the virtual machine.
➢ The same bytecode doesn’t always make python dynamic.
➢ The default prompt for the interpreter is >>>.
➢ The default encoding python source file is UTF-8 (Unicode Transformation Format) .
➢ Unicode standard variable-width character encoding can encode 1,112,064 valid code points using up to four 8-bit bytes.
➢ It uses characters of most languages as strings, literals, comments, and identifiers.
➢ Its standard library uses ASCII characters only.
➢ We add a comment, Windows-1252 encoding as in the file as -
➢ # -*- coding: encoding -*-
➢ # -*- coding: cp1252 -*-
➢ When you want to begin code with a UNIX shebang line:-
➢ #!/usr/bin/env python3
➢ # -*- coding: cp1252 -*-
➢ In the window, the interpreter can find an address like C:\Python36
➢ In Unix /usr/local/bin
➢ We can set the path at the time of installation or using this command:
set path=%path%;C:\python36
On Windows, to run the Python interpreter in the shell, type: $python
➢ To get out of the interpreter in disassembling the Bytecode shell, type: >>> quit()
➢ Alternately, you can use an end-of-file character at the prompt.
➢ Python interpreter exits with a zero exit status.
➢ You can use it in a REPL (Read-Evaluate-Print-Loop) fashion. (Read: take user input.; Eval: evaluate the input.; Print: shows the output to the user.; Loop: repeat.)
➢ if you want, you can save your Python code as a script and execute then using: $python demo.py
➢ To enter interactive mode after running a script, you can pass –i before the script. The command python -c command [arg] … executes statements in command, and the python - m module [arg] … executes the source file for the module.
➢ All command-line options are described in the Command line and environment.
Features of Python Interpreter
➢ Python interpreter offers some features:
❖ Interactive editing
❖ History substitution
❖ Code completion on systems with support for reading line
➢ In the first Python prompt, pressing the Ctrl+P:- Tells you if your interpreter supports command-line editing.
➢ A beep indicates that it does support command-line editing.
➢ Otherwise, it will either perform a no-operation or echo ^p to indicate it isn’t available.
Passing Arguments
➢ You pass a script name and additional arguments to the shell.
➢ Python interpreter turns these into a list of strings and assigns these to the variable argv in the sys module.
➢ The following command will give us a list of thisimport sys
➢ Without a script or arguments, sys.argv [0] denotes an empty string.
➢ A script name of ‘-‘means that it sets sys.argv [0] to ‘-‘, and with ‘-c’, it is set to ‘-c’.
➢ A value of ’-m’ sets sys.argv [0] to the module’s full name.
➢ The command (‘-c’)/ module(‘-m’.) handles the options.
Interactive Mode
➢ In Python interpreter, interactive mode reads commands from a tty (teletype:- terminal control function).
➢ The primary prompt (first ) is the following: >>>
➢ It shows prompts the developer for the next command.
➢ This is the A read–eval–print loop (REPL).
➢ Python interpreter prints a welcome message, version number, and a copyright notice Before it prints the first prompt. $ python3.8Python 3.8 (default, Sep 16 2015, 09:25:04)[GCC 4.8.2]
on Linux type "help", "copyright", "credits" or "license" for more information.>>>
➢ The secondary prompt (Continuation lines) is: …
➢ Continuation lines are needed when entering a multi-line construct.
example:- if statement: >>> if the_world_is_flat = True: ... ("Be careful not to fall off!")
Python Interpreter Working:-
➢ Four things in a REPL:
I. Lexing- The lexer breaks the line of code into tokens.
II. Parsing- The parser uses tokens to generate a structure, as, an Abstract Syntax Tree, to depict the relationship between these tokens.
III. Compiling- The compiler turns this AST (abstract syntax tree) module to help Python applications process trees of the Python abstract syntax grammar. An abstract syntax tree can be compiled into a Python code object using the built-in compile () function.
IV. Interpreting- The interpreter executes each code object. These processes divide into main following parts :-
a. Function Objects & Code Objects
b. The Bytecode
c. Dis-assembling the Bytecode
a. Function Objects & Code Objects
➢ In Python, functions are first-class objects. You can pass them without making a call to them.
>>> def bar(a): x=3 return x+a >>> bar
b. The Bytecode
➢ This is a series of bytes, each of which the interpreter loops through and then makes an execution. Ex:- >>> bar ‘d\x01\x00}\x01\x00|\x01\x00|\x00\x00\x17S’
➢ >>>import dis
➢ >>> dis.dis(bar)
c. Dis-assembling the Bytecode
➢ The dis module supports the analysis of CPython bytecode by disassembling it.
➢ The CPython bytecode taken as an input is defined in the file Include/opcode.h and used by the compiler and the interpreter.
=================================================================
0 Comments