تغطي أسئلة مقابلة Python في عام 2026 كل شيء بدءًا من بناء الجملة الأساسي وحتى أنماط المزامنة المتقدمة وهياكل البيانات وتصميم OOP. سواء كنت تستعد لدور مطور مبتدئ أو منصب كبير في إحدى شركات FAANG، فإن هذا الدليل يغطي الأسئلة الأكثر شيوعًا في مقابلة Python مع إجابات واضحة وموجزة.
📋 Table of Contents
أسئلة بايثون الأساسية
1. ما الفرق بين القائمة والصفوف؟
القوائمقابلة للتغيير – يمكن إضافة العناصر أو إزالتها أو تغييرها.الصفوفغير قابلة للتغيير – بمجرد إنشائها، لا يمكن تعديلها.
# List — mutable
my_list = [1, 2, 3]
my_list.append(4) # OK
my_list[0] = 10 # OK
# Tuple — immutable
my_tuple = (1, 2, 3)
# my_tuple[0] = 10 # TypeError!
# Tuples are faster and use less memory
# Use tuples for heterogeneous data (coordinates, RGB)
# Use lists for homogeneous, modifiable sequences
# Named tuples — readable tuples
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
p = Point(3, 4)
print(p.x, p.y) # 3 4
2. شرح GIL (قفل المترجم العالمي) لبايثون
GIL عبارة عن كائن مزامنة (mutex) يسمح لخيط واحد فقط بتنفيذ كود Python الثانوي في المرة الواحدة، حتى على الأنظمة متعددة النواة. هذا يعني أن سلاسل بايثون لا يمكنها تحقيق التوازي الحقيقي لوحدة المعالجة المركزية مع كود بايثون.
- تقوم الخيوط بإصدار GIL أثناء عمليات الإدخال/الإخراج (الشبكة، القرص) – لذلك يعمل الترابط للمهام المرتبطة بالإدخال/الإخراج
- بالنسبة للتوازي المرتبط بوحدة المعالجة المركزية، استخدم
multiprocessing(كل عملية لها جيل خاص بها) - يحتوي الإصدار Python 3.13+ على الوضع التجريبي الحر (يمكن تعطيل GIL)
3. ما هو مصممو الديكور؟ أعط مثالا.
import functools, time
def timer(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
start = time.perf_counter()
result = func(*args, **kwargs)
elapsed = time.perf_counter() - start
print(f"{func.__name__}: {elapsed*1000:.2f}ms")
return result
return wrapper
@timer
def slow_function():
time.sleep(0.1)
return 42
slow_function() # "slow_function: 100.12ms"
4. ما الفرق بين *args و **kwargs؟
def func(*args, **kwargs):
print(args) # tuple of positional arguments
print(kwargs) # dict of keyword arguments
func(1, 2, 3, name="Alice", age=30)
# (1, 2, 3)
# {'name': 'Alice', 'age': 30}
# Unpacking
def add(a, b, c): return a + b + c
numbers = [1, 2, 3]
print(add(*numbers)) # 6
settings = {"a": 1, "b": 2, "c": 3}
print(add(**settings)) # 6
5. ما هو المولد ومتى تستخدمه؟
# Generator — lazy evaluation, memory efficient
def fibonacci():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
# vs list — loads everything into memory
fib = fibonacci()
print([next(fib) for _ in range(10)])
# Use generators when:
# 1. Processing large datasets (files, DB rows)
# 2. Infinite sequences
# 3. Building pipelines
أسئلة OOP
6. ما الفرق بين @classmethod و @staticmethod؟
class Circle:
pi = 3.14159
def __init__(self, radius):
self.radius = radius
# Regular method — receives instance (self)
def area(self):
return self.pi * self.radius ** 2
# classmethod — receives class (cls), can access class variables
@classmethod
def from_diameter(cls, diameter):
return cls(diameter / 2) # factory method
# staticmethod — receives nothing, utility function
@staticmethod
def is_valid_radius(radius):
return radius > 0
c1 = Circle(5)
c2 = Circle.from_diameter(10)
print(Circle.is_valid_radius(-1)) # False
7. شرح MRO (أمر تحليل الطريقة)
class A:
def method(self): return "A"
class B(A):
def method(self): return "B"
class C(A):
def method(self): return "C"
class D(B, C): # Multiple inheritance
pass
d = D()
print(d.method()) # "B" — follows MRO
print(D.__mro__) # (D, B, C, A, object)
# Python uses C3 linearization for MRO
أسئلة متقدمة
8. كيف تعمل إدارة الذاكرة في لغة بايثون؟
- العد المرجعي– يتتبع كل كائن عدد المراجع التي تشير إليه
- عندما يصل العدد إلى 0، يتم تحرير الكائن على الفور
- جامع القمامة– يتعامل مع المراجع الدائرية (لا يمكن عد المراجع)
gc.collect()– تشغيل جمع البيانات المهملة يدويًا- التدريب— الأعداد الصحيحة الصغيرة (-5 إلى 256) والسلاسل القصيرة يتم تخزينها مؤقتًا
9. ما الفرق بين النسخة السطحية والعميقة؟
import copy
original = [[1, 2, 3], [4, 5, 6]]
# Shallow copy — new list, but nested objects are shared
shallow = copy.copy(original) # or list(original) or original[:]
shallow[0].append(99)
print(original[0]) # [1, 2, 3, 99] — MODIFIED! (shared reference)
# Deep copy — completely independent copy
deep = copy.deepcopy(original)
deep[0].append(88)
print(original[0]) # [1, 2, 3] — unchanged
10. ما هي هياكل البيانات المضمنة في لغة بايثون وتعقيداتها الزمنية؟
| عملية | قائمة | إملاء | set | com.deque |
|---|---|---|---|---|
| وصول | يا(1) | يا (1) متوسط | N/A | على) |
| يبحث | على) | يا (1) متوسط | يا (1) متوسط | على) |
| إدراج (النهاية) | يا(1) الإماتة | يا (1) متوسط | يا (1) متوسط | يا(1) |
| إدراج (بدء) | على) | N/A | N/A | يا(1) |
| يمسح | على) | يا (1) متوسط | يا (1) متوسط | يا(1) |
أسئلة غير متزامنة
11. ما الفرق بين asyncio.gather() وasyncio.TaskGroup؟
import asyncio
# asyncio.gather — partial failures possible
async def example_gather():
results = await asyncio.gather(
task1(),
task2(),
return_exceptions=True # don't cancel others on failure
)
# Results include exceptions if return_exceptions=True
# asyncio.TaskGroup (Python 3.11+) — all-or-nothing
async def example_task_group():
async with asyncio.TaskGroup() as tg:
t1 = tg.create_task(task1())
t2 = tg.create_task(task2())
# If any task raises, all are cancelled — structured concurrency
asyncio.run(example_task_group())
يأتي نجاح مقابلة بايثون من فهم اللغة بعمق، وليس فقط حفظ الإجابات. ركز على “السبب” وراء كل مفهوم – لماذا يوجد GIL، ولماذا تعتبر المولدات ذات كفاءة في الذاكرة، ولماذا تعتبر النسخ العميقة مهمة. اشرح المقايضات وأظهر التطبيق الواقعي لكل مفهوم.
🔗 Share this article
✍️ Leave a Comment