If you write code in Python and enjoy working with this language, then this post is for you. Let’s have an interview and assess how well you understand Python.
Question 1
Question:
Explain why the following code behaves the way it does:
Answer:
Python caches numbers in the range of -5 to 256 source.
You can learn more about memory management in Python by reading the Python Memory Management post.
Question 2
Question:
How does import
work in Python? Why does the following code return the results it does?
Answer:
Python frozen modules ensure that the Python interpreter loads a module into the sys.modules dictionary only once and reuses it afterward. To see this in action, open a Python3 interactive shell (open a terminal, type python3
, and hit enter), and try importing the __hello__
module:
Question 3
Question:
What is __pycache__
, and what are .pyc
files?
Answer:
Whenever you run a Python program, the interpreter compiles your code to bytecode (for efficiency) and stores it in a folder named __pycache__
[source].
If you look inside this folder, you’ll see .pyc
or .pyo
files. Rather than reading and executing the code line by line, the Python interpreter executes the cached .pyc
bytecode in the virtual machine. This process is similar to Java.
Question 4
Question:
What impact does exception handling have on the performance of a program? If it slows down the program, how would you minimize this slowdown?
Answer:
Python uses a stack for exception handling. When an exception occurs, the interpreter looks for the exception handler in the call stack (your try-except
code). If no handler is found, Python terminates the program and prints a traceback in the terminal.
Python encourages the use of EAFP ("it's easier to ask for forgiveness than permission") over LBYL ("look before you leap") style coding. To write efficient and simple code, consider a scenario where users provide a list of numbers [1, 2, 3, 4], and the program calculates the sum. If you are confident that users will input numeric lists 90% of the time, you can use try-except. If not, it’s better to use an if-else statement to handle the control flow (based on personal experience and advice from Core Python Developers).
Question 5
Question:
Why does the following code return False
? Both objects have the same value. How would you correct it?
Answer:
There are two solutions for this.
Solution 1: Using dunder methods. You can learn more by following the link.
Solution 2: The dataclasses module is useful here. If you don’t want to write all the dunder methods yourself, you can use the dataclass decorator, and it will automatically generate the necessary methods for you.
Question 6
Question:
What is the difference between a metaclass and a class? Can you demonstrate creating a metaclass?
Answer:
Classes create objects, and classes themselves are objects. Metaclasses create classes.
Question 7
Question:
We have three different types of lists:
If we create a list with range(1, 10_000_000)
and check the sum using sum()
, we find that the regular list()
computes the sum much faster than the others. Why is it so much faster, and how is it computed so efficiently?
Answer:
This question is for you to figure out. Send your answer to me via email at [email protected].
To make the most of this post, study it and teach your friends (share it)!