In this blog, I have shared some important facts of Python in the form of question and answer.

1. Is Python interpreted or compiled? Are there ways to compile the code?

What’s the Difference? … In a compiled language, the target machine directly translates the program. In an interpreted language, the source code is not directly translated by the target machine. Instead, a different program, aka the interpreter, reads and executes the code.

Python is called an interpreted language. Python doesn’t convert its code into machine code, something that hardware can understand. It actually converts it into something called byte code. So within python, compilation happens, but it’s just not into a machine language. … So we need actually an interpreter called the python virtual machine. Python source code is automatically compiled into Python byte code by the C Python interpreter. The compile() method returns a Python code object from the source.

2. What is exec function and how we can use it?

What does exec() return?

exec() doesn’t return anything itself, it take strings and turn them into executable code. It can be useful, especially if we are the one who controls the input. If we allow users to input a value using exec(input()), the user may issue commands to change file or even delete all the files. So before using any methods inside the exec() function one must keep in mind about what all functions do exec() support. To view this we may use dir() function.

3. What is __call__method in python?

Note that callable() function returns True if the object appears callable, it’s possible that it returns True even if the object is not callable. However, if this function returns False then the object is definitely not callable.

Also, a python class is always Callable. So always use callable() with an instance of the class, not the class itself. Let’s look at a simple example to check this behavior.

Let’s define a class with __call__() function.

4. How can we get the code of a python object?

getsource() method is used to get the source code of Python objects. When we execute this, it simply returns the source code as a single string. An IOError is raised if the source code cannot be retrieved. If the function is compiled from a string, stream or imported from a compiled file, then we cannot retrieve its source code.

We import the inspect module and retrieve the source code for given script as follows:

It simply returns the source code of the function.

5. Which is faster in Python — Searching in a list or a dictionary, and why?

Why is looking up entries in a dictionary so much faster?

It’s because of the way Python implements dictionaries using hash tables. Dictionaries are Python’s built-in mapping type and so have also been highly optimized.

6. What are the ways to make python code work faster?

As a dynamic language, Python will typically perform slower for specific benchmarks than standard implementations of some other languages (although it is faster than plenty of others). Internally, the reason for Python code executing more slowly is that the code is interpreted at runtime instead of being compiled to a native code at compiling time.

So how can we speed up Python’s code?

  • Code profiling identifies which parts of our code are particular bottlenecks, both in terms of computation time and memory usage. When we find a bottleneck, we can try several solutions.
  • If we have a lot of for loops in data computations, the first thing you should try is to vectorize our code using libraries like Pandas (or NumPy, which can be faster).
  • We can also parallelize our code using Python libraries, such as threading and multiprocessing (and there are many high level wrappers that simplify this process, such as concurrent futures).
  • Threading is ideal for more I/O bound operations such as reading from disk or downloading data. Multiprocessing, meanwhile, is more suited to computationally heavy tasks, such as risk calculations.

7. What are metaclasses and dataclasses in python?

Metaclasses aren’t supported by every object oriented programming language. Those programming language, which support metaclasses, considerably vary in way they implement them. Python provides you a way to get under the hood and define custom metaclasses.

A data class is a regular Python class. It is a class typically containing mainly data, although there aren’t really any restrictions.

A data class comes with basic functionality already implemented. For instance, we can instantiate, print, and compare data class instances straight out of the box.

By default, data classes implement a.___repr___() method to provide a nice string representation and an.__eq__() method that can do basic object comparisons.

References:

https://www.programiz.com/python-programming/methods/built-in/exec