Skip to main content

Command Palette

Search for a command to run...

Exploring under the hood of Python

this blog is written to get quick reference of python inner working

Updated
•4 min read•View as Markdown
Exploring under the hood of Python

How python code executes ?

  • hello.py -> byte code -> PVM (python virtual machine)

  • here hello.py is python file and when we run by python3 hello.py it creates byte code which is mostly hidden

some cases like importing from other file make that bytecode file visible inside __pycache__ folder (__pycache__/hello.cpython-313.pyc)

  • hello.cpython-313.pyc file is compiled python bycode

PVM (python virtual machine)

  • there is an interpretation loop that keep running (often referred as evaluation loop) that repeatedly fetches, decodes, and executes bytecode instructions one by one until the program terminates

different types of python

  1. cpython (default python)

  2. jython (python on JVM and for JAVA environment)

  3. PyPy (provides faster executions and better performance)

  4. IronPython (python for .NET environment)

  5. StacklessPython (mostly used for concurrency)

How memory works inside Python?

String

  • Python strings are immutable, so updating a variable to a different string rebinds the variable to another string object; it does not modify the original string. The old object can be garbage-collected when it is no longer referenced. Assigning the same object to another variable (e.g., y = x) does not require creating a copy of that object; both variables refer to the same object. That's why printing y still returns the old string ("Raj"): y continues to refer to the original string object, even after x is reassigned to "Akash".

Numbers:

  • At a = a + 5, Python first evaluates the right-hand side by retrieving the value of a and adding 5, producing 10. The name a is then bound to the resulting integer object. Its previous binding to 5 is removed. If the old object has no remaining references, it may be reclaimed immediately by CPython's reference-counting mechanism.

  • For computationally intensive tasks, we can use specialized libraries to perform calculations on the CPU or GPU, depending on the library's capabilities and the hardware available. (libraries such as NumPy, PyTorch)

List:

  • List in python is mutable so here things work little differently

  • this video explain concept in practical way

Shallow Copy with slicing:

  • a = [1,2,3]

  • b = a[:] Here, we are not providing a start or stop index in slicing, so Python creates a shallow copy of the entire list.

  • Because a and b refer to different list objects, modifying the list structure of one (such as adding, removing, or replacing an element) does not affect the other.

  • However, this is a shallow copy. If the list contains nested lists or dictionaries, those inner objects are still shared between a and b , to understand more explore difference between append() method and directly replacing value of List.

  • also explore copy package and working of copy.copy() method and copy.deepcopy()

is vs == :

  • == checks whether two objects have equal values

  • is checks whether two variable refer to exact same memory object

is :

>>> m = [1,2,3]
>>> n = [1,2,3]
>>> m is n
False

False because both variable refer to different memory objects.

>>> j = [1,2,3]
>>> k = j
>>> j is k
True

True because both variable refer to same memory object.

== :

>>> a = [4,5,6]
>>> b = [4,5,6]
>>> a == b
True

True because both lists contain the same values in the same order.

>>> a = [4,5,6]
>>> b = [4,5,7]
>>> a == b
False

False because the lists contain different values. The last elements (6 and 7) are different.