Updated: Jun 15, 2020

Bytecode display

Consider the following python program:

print("Multiplication of 4 and 5 is ",c)

You can type this program in a text editor such as Notepad and save it in the sense that you pay first. The 'first.py' file contains the source code.

Then use the Python compiler to compile the program.

C:/>python first.py

The result is displayed as follows.

Total = 20

It's okay. But you don't need the output of the program. I would like to see the bytecode instructions created internally by the Python compiler before being executed by PVM. For this purpose, when using Python commands, you need to specify the dis module as follows:

C:\>python -m dis first.py

By it produce this output:

2		0 LOAD_CONST		0 (4)
		3 DUP_TOP	
		6 LOAD_CONST		1 (5)
		10 STORE_NAME		0 (a)
		13 STORE_NAME		1 (b)
		16 LOAD_NAME		0 (a)
		19 LOAD_NAME		1 (b)
		25 LOAD_CONST		3 (c)
3		26 LOAD_CONST		2 (print)
		29 LOAD_CONST		1 ('Multiplication of 4 and 5 is ')
		32 LOAD_CONST		3 (c)
		33 CALL_FUNCATION	2 (2 positional, 0 keyword pair)
		36 POP_TOP		

Python flavor

Python flavors refer to different types of Python compilers. These flavors help integrate various programming languages ​​into Python. Below are some of them.

  • CPython: This is a standard Python compiler implemented in C. This is Python software that programmers download and use directly from https://wunw.python.org/dounloads/. In this case, all Python programs are internally converted to bytecode using C language functions. This bytecode is a nun in the interpreter available in the Python Virtual Machine (PVM) written in C. The advantage is that you can run C and C++ functions and programs in CPython.

  • Jython: This is Curly, known as JPython. This is an implementation of the Python programming language designed to run on the Java platform. The Jython compiler first compiles a Python program into Java bytecode. This bytecode is executed by the Java Virtual Machine (JVM) and produces output. Jython contains libraries that are useful to both Python and Java programmers. It can be downloaded from http://wnww.jython.org/.

  • IronPython: This is another implementation of the Python language for the .NET framework. It is written in C# (C Sharp) language. Compiling a Python program produces an intermediate language (IL) that runs in the common language runtime (CLR) and produces output. This Python flavor gives you the flexibility to use both .NET and Python libraries. IronPython can be downloaded from http://ironpython.net/.

  • PyPy: This is an implementation of Python using the Python language, written in the Python language written in Python. RPython is suitable for writing language interpreters. With the addition of the JIT (Just In Time) compiler to PVM, PyPy programs run very fast. PyPy can be downloaded by visiting the page at http://pypy.org/download.html.

The original Python uses only the interpreter in the Python Virtual Machine (PVM), which slows down the execution of Python programs. To improve the execution speed, a compiler called JIT (Just in Time) is introduced in PyPy's PVM. Therefore, PyPy programs run faster than Python programs. PyPy can be downloaded from http://pypy.org/download.html.

  • StacklessPython: Small tanks that need to be run individually are called tasklets. Tasklets run independently on the CPU and can communicate with other users through channels. A channel is a manager that schedules, controls, and suspends tasklets. A thread is a process that executes hundreds of such tasklets. You can create threads and tasklets in StacklessPython, which is a reimplementation of the original Python language. It can be downloaded from https://pypi.python.org/pypi/stackless-python/10.0.

  • Pythonxy: This is pronounced Python xy and written as Python (X, Y). This is the Python implementation that you get after adding science and technology packages. Download Pythonxy from https://python-xy.github.io/downloads.html

  • AnacondaPython: When Python is redeveloped to handle large-scale data processing, predictive analytics, and scientific computing, it's called ArcondaPython. This implementation focuses primarily on large amounts of data. It can be downloaded from https: //www.continuum.io/downloads.

Python Virtual Machine (PVM)

We know that computers understand only machine code consisting of 1s and 0s. Computers only recognize machine code, so you must convert your program to machine code before sending it to your computer for execution. You should use the help of a compiler for this purpose. Compilers usually translate the source code of a program into machine code. Python compilers perform the same task, but in a slightly different way. Converts the program source code into another code called bytecode. Each Python program statement is translated into a group of bytecode instructions. Then what is bytecode? Bytecode represents a set of fixed instructions written by Python developers that represent all types of operations. These are called bytecode instructions because the site of each bytecode instruction is 1 byte (or 8 bits). According to the Python organization, new instructions may be added to existing bytecode instructions at any time. You can find the bytecode instructions in the .pyc file.

The role of the Python Virtual Machine (PVM) is to translate bytecode instructions into machine code so that the computer can execute those machine code instructions and display the final output. To perform this conversion, PVM is equipped with an interpreter. The interpreter translates the bytecodes into machine code and sends the machine code to a computer processor for execution. The Python virtual machine is also called an interpreter because the interpreter plays a major role.

Frozen binary

When developing software in Python, there are two ways to make it available to end users. The first way is to provide the .pye file to the user. The user installs PVM on his computer and executes the bytecode instructions in the .pye file.

Another way is to provide the .pye file, PVM, and the necessary Python libraries. This method will convert all .pye files, associated python libraries, and PVM into a single executable, typically with an exe extension, so the user can double-click the file to run it directly . This conversion of a Python program into a real executable file is called a frozen binary. However, frozen binaries also include PVM and library files, which makes them larger than simple .pye files.

Other party software must be used to create frozen binaries. For example, py2exe is software that produces frozen binaries for Windows operating systems. pyinstaller can be used on UNIX or LINUX. Freeze is another program from the Python organization that produces frozen binaries for UNIX.

Memory management in Python

In C or C#, the programmer must dynamically allocate and deallocate (or deallocate) memory at run time. For example, programmers use the malloc() function to allocate memory and the free() function to deallocate memory. However, in Python, memory allocation and deallocation are done automatically at runtime. The programmer does not have to allocate memory when creating an object or deallocate memory when deleting an object. PVM in Python handles such issues.

Everything is considered an object in Python. For example, a string is an object and a list is an object. Functions are objects. Modules are also objects. Memory must be allocated for all objects. The memory manager in PVM allocates the required memory for objects created by Python programs. All of these objects are stored in a separate memory called the heap. The heap is memory that is allocated at run time. The size of heap memory depends on your computer's random access memory (RAM) and can be increased or decreased depending on your program requirements.

It is known that the actual memory (RAM) of a program is allocated by the underlying operating system. At the top of the operating system, the low memory allocator monitors whether enough memory is available to store the object. Above the raw memory allocator are several objects. Specific allocators run on the same heap. These memory allocators implement different types of memory management policies, depending on the type of object. For example, integers must be stored in memory one way and strings must be stored another way. Similarly, when dealing with tuples and dictionaries, they need to be stored differently. These issues are handled by the object-specific memory allocator.

Garbage collection in Python

Modules represent Python code that performs specific tasks. The garbage collector is a Python module that helps you to remove unused objects in memory from your program. The name of the module that represents the garbage collector is gc. The easiest way to keep a count of each object as to how many times it has been referenced (or used) by the garbage collector. If an object is referenced twice, its reference count will be 2. If an object has some count, the garbage collector will not remove it from memory because it is being used programmatically. If the object is found with a reference count of 0. The garbage collector understands that the object is unused by the program and can be removed from memory. Therefore, the memory allocated to that object is deallocated or deallocated.

The garbage collector can detect reference cycles. A reference cycle is a cycle of references that paint from the last object to the first. For example, suppose you have three objects A, B, and C. Object A references object B and object B holds a reference to object C. If the object references the first object A, the objects form a reference cycle. Figure 1.6 shows a reference cycle for three objects.

Even if objects A, B, and C are no longer used in your Python program, they still contain one reference to each. The reference count for each object is 1, so the garbage collector does not remove these objects from memory. These objects remain in memory even after the program has finished running. To avoid this. The garbage collector uses an algorithm (logic) to detect the reference cycle and remove the objects in the cycle.

The garbage collector classifies objects into three generations. The newly created object is considered a Generation 0 object. If the garbage collector first looks at the object in memory and doesn't remove it from memory because the program is using it, it will be placed in the next generation. This is Generation 1. If the garbage collector plans to delete the object for the second time and the object still exists for the second time, the old object belongs to generation 2 because the object is placed in generation 2. The garbage collector tries to delete the young object, which is not the old object and is not referenced programmatically.

The navigator of the garbage collector is automatically. Python schedules the garbage collector according to a number called a threshold. This number represents the number of times the garbage collector was deleted to collect objects. If the number of allocations minus the number of trading locations is greater than the threshold number, then the garbage collector will run automatically. You can use the method get threshold () of the gc module to find out the number of thresholds.

If more objects are created and the system runs out of memory, the automatic garbage collector will not run. Instead, the Python program throws an exception (runtime error). If the programmer is confident that his program does not contain reference cycles, the automatic garbage collector is the best choice.

If your program finds reference cycles, it is recommended that you run the garbage collector manually. You can use the gc module's collect () method for this purpose. Manual garbage collection can be done in two ways: time-based and event-based. When the garbage collector is called at a specific time interval, it is called time-based garbage collection. If the garbage collector is called based on an event (for example, when the user disconnects from the application), it's called event-based garbage collection, but running the garbage collector too often slows down your program.

Points to remember

  1. Python was developed in 1991 by Guido Van Rossum.

  2. Python is a high-level programming language with the features of functional programming languages ​​such as C and object-oriented programming languages ​​such as Java.

  3. In object-oriented terminology, an object represents a physical entity that contains a behavior. The behavior of an object is represented by the attributes of properties and actions. Attributes are represented by variables and actions are performed by functions or methods.

  4. In object-oriented terminology, a class is an abstract concept that describes the general behavior of some object. Classes represent behavior and do not physically exist. Behavior is represented by attribute variables and actions (functions). Therefore, the class also contains variables and functions.

  5. Groups of objects that have the same behavior belong to the same class

  6. It's called Python because the standard Python compiler is written in C.

  7. Other flavors of Python include Jython, Iron Python, and PyPy.

  8. A Python program contains source code that is first compiled by a Python compiler to produce bytecode. This bytecode is passed to the Python Virtual Machine (PVM). PVM translates the bytecode into machine code, which the processor executes and displays the results.

  9. Python bytecode is a set of instructions created by the Python development team to represent all types of operations. Name bytecode is used because each bytecode occupies one byte of memory.

  10. The Python Virtual Machine (PVM) is software that includes an interpreter that translates bytecodes into machine code, depending on the operating system and hardware of the computer system on which the Python program runs.

  11. Python is called an interpreted language because standard PVM is only an interpreter

  12. PVM is most often called the Python interpreter.

  13. PyPy's PVM includes a compiler in addition to an interpreter. This compiler, called the Just in Time (JIT) compiler, helps speed up the execution of Python programs.

  14. Programmers don't have to allocate or deallocate memory in Python. It is PVM's responsibility to allocate and deallocate memory for Python programs.

  15. The memory manager is a module for PVM subprograms that allocate memory to objects. The garbage collector is another module of PVM that frees memory for unused objects).

  16. The programmer does not have to call the garbage collector. When a Python program runs in memory, it runs automatically. In addition, the programmer can call the garbage collector if desired.

  17. A file that contains a Python program with a Python compiler and libraries that you can execute directly is called a frozen binary.

  18. The'py_compile' module translates a Python source file into a pyc file containing bytecode instructions. The .pye file is typically provided to end users.

  19. Given a Python source file, the'dis' module displays the equivalent bytecode instructions in human-readable form.

20 views0 comments

Recent Posts

See All
  • Facebook
  • Instagram
  • Twitter
  • YouTube
  • Pinterest
  • Tumblr Social Icon

© Copyright MH35.ONLINE . All Rights Reserved

Designed by Krushna_005