Is Python interpreted or compiled ? Are there ways to compile the code ?
Yeah ! Python is an interpreted language ( it means a computer program that translates and executes, line by line, a program written in a high level language), although compilation is a step. Python code, written in .py file is compiled to what is called bytecode and stored in .pyc format. This bytecode is a low level set of instructions that can be executed by an interpreter. The bytecode compilation happened internally, and almost completely hidden from developer. Bytecode instructions are executed on Virtual Machine.
Which is faster in python — Searching in a list or a dictionary. And Why ?
Searching in a dictionary is way much faster than searching in a list, it is bcz Python implements dictionaries using hash tables.
Dictionaries are Python’s built in a mapping type and so have also been highly optimized, list requires walking through the list until it finds the result from beginning to the result each time .However, if the data sets are really small (<1000 elements) list perform well.
How can we get the code of a python object ?
We can get the source code of a python object ,we have inspect module a built-in standard library in Python. getsource() method is used to get the source code of Python objects. It returns the text of the source code of an object. The argument may be a module, class, method, function, or code object.
What are the ways to make python code work faster ?
Use list comprehensions : They are concise and speedy way to create new lists.
Use “in” if possible : To check if membership of a list, it’s generally faster to use the “in” keyword.
Use Sets and unions : Too much looping makes your code unhealthy, if you wanted to get the overlapping values in two lists. You could do this using nested for loops.
Use multiple assignments : Python has good way to assign the values of multiple variables.
Use join() to concatenate strings : In python, you can concatenate strings using “+” . However, strings in Python are immutable, and the “+” operation involves creating a new string and copying the old content at each step . A more efficient approach would be to use the array module to modify and then use the join() function to re-create your final string.
Use builtin functions and libraries : Python comes with a lot of batteries included. You can write quality and efficient code but it’s hard to beat the underlying libraries. These have been optimized and are tested.
What is exec function and how we can use it ?
exec() function is used for the dynamic execution of Python program which can either be a string or object code. If it is a string, the string is parsed as a suite of Python statements which is then executed unless a syntax error occurs and if it is an object code, it is simply executed.
exec(object[, globals[, locals]])
It can take three parameters:
- object : As already said this can be a string or object code
- globals : This can be a dictionary and the parameter is optional
- locals: This can be a mapping object and is also optional
'print("The sum of 5 and 10 is", (5+10))'
The sum of 5 and 10 is 15
What are metaclasses and dataclasses in python ?
Metaclass is a class whose instance classes. Like an “ordinary” class defines the behavior of the instances of the class, a metaclass defines the behavior of classes and their instances.
Detaclasses module is introduced in Python 3.7 . A dataclass is a class typically containing mainly data, although there aren’t really any restrictions. It is created using the new @dataclass decorator. Dataclasses automatically create __init__, __repr__, __eq__, __gt__, __lt__ methods without you having to add them explicitly. This is handy when we need to create custom containers for your data.
__call__ method in python ?
Python has a set of built-in methods and __call__ is one of them. Python object is called callable if they define __call__() function. The __call__ method enables Python programmers to write classes where the instances behave like functions and can be called like a function.
Thanks for reading. I hope that this was helpful!👍