VWhen would you choose Python over a statically typed language?
سؤال المقابلPython
يحب محاورو Python السؤال عن أشياء تبدو بسيطة لكن وراءها سببًا: لماذا يمكن أن يكون الـ tuple مفتاحًا في قاموس، وماذا يفعل المولّد (generator) حقًّا، ولماذا تتصرف الخيوط في Python بخلاف لغات أخرى. الإجابة الجيدة تشرح الآلية لا القاعدة فقط.
استخدم الشكل نفسه لكل إجابة: تعريف من جملة واحدة، ومثال قصير، وملاحظة عن متى تستخدمه في مشروع حقيقي. وحيث يعتمد السلوك على إصدار Python فقل ذلك، وقل أي إصدار استخدمت.
- ✓كلمات → عبارات → جمل → تحدث
- ✓معنى الإجابة
- ✓اللغة
أكثر الأسئلة تكرارًا ونماذج الإجابات
What is the difference between a list and a tuple, and when would you use each?
ما الفرق بين القائمة (list) والـ tuple، ومتى تستخدم كلًّا منهما؟
ما الذي يريد المحاور سماعه. قابلية التغيير (mutability)، والقابلية للتجزئة (hashability) نتيجةً لها، وعدم قابلية التغيير السطحية (shallow)، واختيار مبني على المعنى لا على السرعة.
نموذج إجابة بالإنجليزية للتدرّب عليه بصوت عالٍBoth are ordered sequences. The main difference is that a list is mutable and a tuple is immutable. I can change, add or remove items in a list, but not in a tuple. Because of this, a tuple of immutable items is hashable, so it can be a dictionary key or a set member, and a list cannot. I use a list for a collection that grows or changes, like a list of results. I use a tuple for a fixed record, like coordinates or a returned pair of values. Remember that immutability is shallow: a tuple that contains a list can still change that list. I choose by meaning, not by speed.
What is a generator, and why would you use one?
ما المولِّد (generator)، ولماذا تستخدمه؟
ما الذي يريد المحاور سماعه. التقييم الكسول (lazy evaluation) مع yield، وتوفير الذاكرة، والصلة بـ iterator protocol، وحدّ الاستخدام لمرة واحدة.
نموذج إجابة بالإنجليزية للتدرّب عليه بصوت عالٍA generator is a function that uses yield to produce values one at a time instead of returning a full list. It keeps its state between calls and computes each value only when it is requested. This is lazy evaluation, and it saves memory: I can process a very large file line by line without loading all of it. Under the hood, it follows the iterator protocol, with the special methods dunder-iter and dunder-next, which a for loop calls for me. There is also a short syntax called a generator expression, with round brackets. The limit is that a generator can be used only once. After it is exhausted, I must create a new one.
What is the GIL, and how do you handle concurrency in Python?
ما GIL، وكيف تتعامل مع التزامن في Python؟
ما الذي يريد المحاور سماعه. ما يفعله القفل في CPython، والفرق بين الأعمال المرتبطة بالإدخال والإخراج (I/O-bound) والمرتبطة بالمعالج (CPU-bound)، والأداة المناسبة لكل منهما.
نموذج إجابة بالإنجليزية للتدرّب عليه بصوت عالٍThe GIL, or global interpreter lock, is a lock in the standard CPython interpreter. It allows only one thread to run Python bytecode at a time. So threads do not give real parallelism for CPU-heavy work in pure Python. But for I/O-bound work, like network calls, threads and asyncio work well, because the lock is released while the program waits. For CPU-bound work, I use multiprocessing, where each process has its own interpreter, or I move the heavy part to a library with native code, such as NumPy. Newer Python versions are adding experimental free-threaded builds, but I would check the current status before relying on that.
What is a decorator, and how would you write one?
ما المُزخرِف (decorator)، وكيف تكتب واحدًا؟
ما الذي يريد المحاور سماعه. الدوال كائنات من الدرجة الأولى (first-class)، وفكرة الإحاطة (wrapping)، وحالات استخدام حقيقية، وعادة استخدام functools.wraps.
نموذج إجابة بالإنجليزية للتدرّب عليه بصوت عالٍA decorator is a function that takes another function and returns a new function, usually one that wraps the original with extra behaviour. The syntax with the at sign is only a shortcut: decorating a function means passing it to the decorator and assigning the result to the same name. This works because functions are first-class objects in Python. I use decorators for cross-cutting things like logging, timing, caching, retries and access checks. A good habit is to use functools.wraps inside the decorator, so the wrapped function keeps its name and its documentation. A decorator with its own arguments needs one more level: a function that returns the decorator.
How do you keep a Python project reproducible and maintainable?
كيف تُبقي مشروع Python قابلًا لإعادة الإنتاج وسهل الصيانة؟
ما الذي يريد المحاور سماعه. البيئات الافتراضية (virtual environments)، وتثبيت إصدارات الاعتماديات، ودور تلميحات الأنواع (type hints) ومدقق ساكن في الفريق.
نموذج إجابة بالإنجليزية للتدرّب عليه بصوت عالٍI give every project its own virtual environment, so its dependencies do not conflict with other projects or with the system Python. I create it with the built-in venv module, or with a tool such as Poetry or uv, and I never install project packages globally. I pin the dependency versions in a lock file or a requirements file, so a colleague and the CI server get exactly the same environment. For type hints, I add annotations to function signatures and run a checker such as mypy or pyright in CI. Python does not enforce the hints at runtime, but the checker finds many mistakes before the code runs. It also makes the code easier to read.
كيف تنطق المصطلحات الأساسية
- Python
- بَايْثُون
- tuple
- تَبْل أو تُوبْل
- mutable
- مْيُوتَبِل
- dunder
- دَنْدَر (اختصار «double underscore»)
- generator
- جِنَرَيْتَر
- decorator
- دِكَرَيْتَر
- GIL
- غِلْ (بغين صلبة كحرف g)، أو جَيْ آيْ إِل
- venv
- ڤِي إِنْڤ
عبارات مفيدة للإجابة
- In Python, everything is an object.في Python، كل شيء كائن.
- It depends on whether the work is I/O-bound or CPU-bound.هذا يعتمد على ما إذا كان العمل مرتبطًا بالإدخال والإخراج (I/O-bound) أم بالمعالج (CPU-bound).
- A tuple of immutable items can be a dictionary key.يمكن لـ tuple من عناصر غير قابلة للتغيير أن يكون مفتاحًا في قاموس.
- I would use a generator here to save memory.كنت سأستخدم مولِّدًا (generator) هنا لتوفير الذاكرة.
- That is a convention in Python, not a rule of the language.هذا عُرف في Python، وليس قاعدة في اللغة.
- I would check the documentation for the current behaviour.كنت سأراجع الوثائق لمعرفة السلوك الحالي.
- In practice, I would pin the dependencies.عمليًّا، كنت سأثبّت إصدارات الاعتماديات.
أخطاء شائعة
- قول "Python is slow" والتوقف. اشرح أين يذهب الوقت، في المعالج أم في الانتظار، وماذا تفعل: تُجري تحليل أداء (profiling) أولًا، وتختار بنية البيانات المناسبة، وتنقل الكود الساخن إلى مكتبات أصلية (native).
- قول "the GIL means Python has no threads" أو "threads are useless". تظل الخيوط مفيدة في الأعمال المرتبطة بالإدخال والإخراج. الحدّ هو التوازي في أعمال المعالج بلغة Python الصرفة.
- كتابة دالة بوسيط افتراضي من نوع list دون معرفة المشكلة. القيمة الافتراضية تُنشأ مرة واحدة، فتُشارَك بين الاستدعاءات. استخدم None قيمةً افتراضية وأنشئ القائمة داخل الدالة.
- قول "I am working with Python since five years". هذه ترجمة حرفية من العربية «أعمل بـ Python منذ خمس سنوات». استخدم المضارع التام مع for: "I have worked with Python for five years". واستخدم since فقط مع نقطة بداية، مثل "since 2019".
- تسمية القائمة "an array". في Python، الـ array نوع مستقل في المكتبة القياسية. قل "list" أو "sequence" ليعرف المحاور ما تقصد.
الأسئلة الشائعة
أي إصدار من Python أذكر؟
الإصدار الذي استخدمه مشروعك الأخير، مع ميزة واحدة أعجبتك، مثل تلميحات الأنواع (type hints) أو فئات البيانات (dataclasses) أو مطابقة الأنماط البنيوية (structural pattern matching) في الإصدارات الحديثة. لا تدّعِ ميزات إصدارات لم تستخدمها. قول "I would check the release notes" أفضل من التخمين.
هل أحتاج إلى معرفة asyncio؟
في وظائف الـ backend وخدمات البيانات، اعرف الأساسيات: الكوروتينات (coroutines)، وحلقة الأحداث، وawait، ومتى تفيد (انتظارات شبكية كثيرة) ومتى لا تفيد (أعمال ثقيلة على المعالج). مثال حقيقي واحد من عملك أقيم من تعريف طويل.
هل سأكتب كود Python أثناء المقابلة؟
غالبًا نعم: دالة صغيرة أو مهمة بيانات. قل خطتك أولًا، واذكر الحالات الحدية مثل المدخلات الفارغة، واستخدم الأدوات المدمجة مثل enumerate وzip والاشتمالات (comprehensions) ووحدة collections. وفي النهاية قل كلفة حلّك في الزمن والذاكرة.
قراءة الإجابة ليست مثل قولها. تدرّب على كل سؤال بصوت عالٍ، ثم قارن معنى إجابتك ولغتها بالنموذج.
جرّب SpeakRole ↗موضوعات أخرى في هذا المجال