Python — Q/A
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?
First we understand the difference between Interpreted vs Compiled Programming Languages:
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?
exec() is one of the Built-in-functions of Python. It executes the dynamically created program, which is either a string or a code object. The exec() function accepts large blocks of code, unlike the eval() function which only accepts a single expression.
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?
The __call__ method enables Python programmers to write classes where the instances behave like functions and can be called like a function. When the instance is called as a function; if this method is defined, x(arg 1, arg 2, …) is a shorthand for x.
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?
It is sometimes important to know what some functions source codes look like. In such cases, we have inspect module a built-in standard library in Python programming. It provides several useful functions to track information about live objects such as modules, classes, methods, functions, tracebacks, frame objects, and code objects.
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?
Looking up items in a Python dictionary is much faster than looking up items in a Python list.
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?
Is Python a slow language?
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?
Here are the few ways to increase the performance of Python 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?
A metaclass in Python is a class of a class that defines how a class behaves. A class is itself an instance of a metaclass. Like an “ordinary” class defines the behavior of the instances of the class, a metaclass defines the behavior of classes and their instances.
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.
Data Classes in Python 3.7+ (Guide) - Real Python
One new and exciting feature coming in Python 3.7 is the data class. A data class is a class typically containing…
How to retrieve source code from Python objects? - GeeksforGeeks
How to retrieve source code from Python objects? It is sometimes important to know what some functions source codes…
The syntax of compile() is: compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1) compile() method…