Fundamentals 7 min read

Top 10 Python Interview Questions Every Developer Must Master

This article compiles ten frequently asked Python interview questions covering class inheritance, callable objects, __new__ vs __init__, list and dict generation, variable scope, swapping values, default methods, package imports, closures, and performance pitfalls, providing concise explanations and code illustrations.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Top 10 Python Interview Questions Every Developer Must Master

Overview

Python is a very popular programming language, and with the recent growth of machine learning, cloud computing and other technologies, the demand for Python positions has risen sharply. Below are ten questions that Python interviewers often ask, provided for reference and study.

1. Class Inheritance

Question: How to call the show method of class A in the following code?

Answer: Use the __class__ attribute to reference the class object, assign it to type A, then call show. Remember to restore the original class after the call.

2. Callable Objects

Question: What code needs to be added so that the following snippet can run?

Answer: Implement the __call__ method so that an instance can be invoked directly.

3. __new__ and __init__

Question: What does the following code output?

Answer:

Using the __new__ method allows you to decide which object to return before the object is created, which is useful for design patterns such as singletons or factories. __init__ is called after the object is created.

4. List and Dict Generation

Question: What does the following code output?

Answer:

5. Global and Local Variables

Question: What does the following code output?

Answer:

The variable num is not global, so each function gets its own copy. To modify the global num, you must declare it with the global keyword, e.g.:

6. Swapping Two Variables

Question: Swap the values of two variables in a single line of code.

Answer:

7. Default Method Handling

Question: In the following code, add code so that undefined methods invoke mydefault and produce the expected output.

Answer:

The __getattr__ method is invoked only when an undefined attribute or method is accessed. By adding a *args parameter to mydefault, it can accept any arguments passed to undefined methods.

8. Package Management

Question: A package contains three modules mod1.py, mod2.py, mod3.py. When using from demopack import *, how to ensure only mod1 and mod3 are imported?

Answer: Add an __init__.py file and include:

9. Closures

Question: Write a function that takes an integer n and returns another function that multiplies its argument by n and returns the result.

Answer:

10. Performance

Question: Identify the performance bottleneck in the following code.

Answer: In Python, str objects are immutable; each iteration creates a new str object to store the new string. As num grows, more str objects are created, increasing memory consumption and slowing execution.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonprogrammingCoding Questions
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.