Scratching Python from the base

Naveen Singh
7 min readJan 1, 2021

Is Python interpreted or compiled?

Before this, let’s understand difference between compiler and interpreter.

Compiler: Compilation involves translating your human understandable code to machine understandable code, or Machine Code. Machine code is the base level form of instructions that can be directly executed by the CPU. Upon successful compilation, your code generates an executable file. Executing this file runs the operations in your code step by step.

Interpreter: it converts source code written by the developer into intermediate language which is again translated into the native language / machine language that is executed.

Let understand by a diagram:

Machine code is low level language where C, java, python are example of high level language. Here when it is converted into machine code then a executable file is generated. Executing this file will run the program.

Interpreter of python

For the most part, Python is an interpreted language and not a compiled one, although compilation is a step. Python code, written in .py file is first compiled to what is called bytecode which is stored with a .pyc or .pyo format. This bytecode is a low-level set of instructions that can be executed by an interpreter. Instead of executing the instructions on CPU, bytecode instructions are executed on a Virtual Machine. For more, Click here.

What should be used in python for searching operation? Dictionary or list.

Dictionary is far better than list for searching, inserting and deleting data because it uses hash lookups behind the scene. Hash table works on hash function that is the keys are assigned by hash function to the index. In other words, keys become the index.

List can be used if the set is small but in large dataset it has to traverse from the beginning to the last to find element.

Hash Function working. Credits

How can we get the code of a python object?

To get the source of object in python we can use “inspect.getsource(object_name)”

input
output

Ways to make Python code work faster

First, understand why always python is compared to a snail.

Python works on interpreter(i know, i also told that its get compiled first),the source code gets converted into bytecode then goes on python virtual machine. The PVM sounds more impressive than it is!

It’s just a big loop that iterates through your byte code instructions, one by one, to carry out their operations. The PVM is the runtime engine of Python; it’s always present as part of the Python system, and is the component that truly runs your scripts. Technically, it’s just the last step of what is called the Python interpreter.

So, this is how a python interpreter runs your python code!

As you can see how many steps are involved. That is the one of the reason why python is slow. So, it is slightly incorrect to say that the language is slow, rather it is the context in which it is running that is slow.

How to overcome this?

  1. Understand and use the built in function of python.
  2. import only those module that is needed. For example, you want to use sqrt function so instead of “import math” use “from math import sqrt”.
  3. To concatenate string don’t use + every time because strings in python are immutable and can not be modified. So, whenever the string is concatenated python create a new string and substring and return new string.
  4. Limit the use of loops instead of this use list comprehension, dictionary comprehension, generators and decorators.
  5. Use of map function to change the data type of iterator’s elements. map returns generators because inside map function instead of return “yield” is used.
  6. Generators return one item at a time rather than returning all item at a time. It saves the process timing and memory too.
  7. Use of JIT(just in time) compiler. A JIT compiler is a feature of the run-time interpreter, that instead of interpreting bytecode every time a method is invoked, will compile the bytecode into the machine code instructions of the running machine, and then invoke this object code instead.

What is exec function and how can we use it?

exec(source, globals=None, locals=None) is a built in function or method that is use to execute python code dynamically. this can be a string or some object code. When it is a string, Python parses it as a set of statements and executes it if there is no syntax error. When it is object code, Python executes it. But exec() doesn’t return a value; it returns None. Hence, we cannot use return and yield statements outside function definitions.

Global can restrict the feature of exec() where local restrict the local functionality .Execute the given source in the context of global and locals.

In laymen term : exec() just execute a python statement just like interpreter do for you. For example:

Here we can see statement itself contain a python code that can be executed by exec(). The benefit of exec() is it gives the freedom to the user to run their own python statement and the biggest disadvantage is user can manipulate the statement that is if user import the OS module then he can delete all the files from the system. For more click here.

Metaclasses and dataclasses in python:

Dataclasses: It is a a utility tool to make structured classes specially for storing data. These classes hold certain properties and functions to deal specifically with the data and its representation.

Whenever you see a class which store data has a function called __init__(self,other argument) like this. But what about that user defined classes who don’t have this class. Here dataclasses comes in play. It provide a structure to class for storing data.

as you can see error occur

In this example what we actually want is we just pass the argument of the datatype mentioned in class and it get store there. Now we will gonna use dataclasses.

dataclass is a decorator that is used to add generated special methods to classes. It decorate the function Fun() and represent it in a structural form that means dataclass contains both __init__() and __repr__().

It can also be wriiten as :

Fun = dataclass(Fun(“naveen”, “xyz”, “hindi”, 1)); Fun()

Metaclasses:

  1. Class: Class are like blueprint of an object that defines rules, attributes, and what operation can be performed. For example: You want to construct a exact copy of Eiffel tower, So must need the blueprint of the tower to create the same or you have to create the architecture from the starting. Here Eiffel Tower is your object and blueprint is the class from which the object is to made.
  2. If you have your own design then you can change some architecture that is you have your own created blueprint for an object. This is where metaclasses comes in play. Metaclass defines rules for the class.
  3. To understand more clearly we have to remember that is python everything is “object”.

You can see the type of Try that is “type”. “type” is responsible for defining rules and create this class for us.

Let’s see how:

I used type(“Name”,(Parent class or inherited class),{“attributes” : attribute}) and also created object for the same for verification.

I inherited a class “Trial”. This process is called inheritance in OOPs concept.

In class Try’s attribute metaclass is directed to our own metaclass to change the rules. __new__ create the instance of the class and returns type(class_name, bases, attributes). You can also compare this with type(“Try”, (Trial,), {“x” : 6})

As you can see in above image without making the instance of the class, the output displayed all the attribute. This is what metaclass do.

Another Example:

But what this means? It changed the rules like this:

t is the object or instance of Try, but why an error occur when i called x variable like print(t.x). I have an a variable named x(highlighted in yellow) but why it is showing that it has no attribute “x”?

This happened because instead of returning attribute we returned blank{}. This is how we can change the rules and create new one.

If there is any error please comment down below.

Thanking you for reading

--

--