VWhen would you choose Python over a statically typed language?
Senso della risposta✓
Lingua2
Domanda dell’intervistatorePython
A chi intervista su Python piace chiedere cose che sembrano semplici ma hanno una ragione sotto: perché una tupla può essere la chiave di un dizionario, che cosa fa davvero un generatore e perché i thread si comportano in modo diverso rispetto ad altri linguaggi. Una buona risposta spiega il meccanismo, non solo la regola.
Usa la stessa forma per ogni risposta: una definizione di una frase, un breve esempio e una nota su quando la useresti in un progetto reale. Se il comportamento dipende dalla versione di Python, dillo e di' quale versione hai usato.
- ✓Parole → espressioni → frasi → parlato
- ✓Senso della risposta
- ✓Lingua
Domande comuni e risposte di esempio
What is the difference between a list and a tuple, and when would you use each?
Qual è la differenza tra una lista e una tupla e quando useresti ciascuna?
Che cosa vuole sentire l'intervistatore. La mutabilità, l'hashability come conseguenza, l'immutabilità superficiale e una scelta basata sul significato, non sulla velocità.
Risposta di esempio in inglese da esercitare ad alta voceBoth 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?
Che cos'è un generatore e perché ne useresti uno?
Che cosa vuole sentire l'intervistatore. La valutazione lazy con yield, il risparmio di memoria, il legame con il protocollo degli iteratori e il limite dell'uso singolo.
Risposta di esempio in inglese da esercitare ad alta voceA 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?
Che cos'è il GIL e come gestisci la concorrenza in Python?
Che cosa vuole sentire l'intervistatore. Che cosa fa il lock in CPython, la differenza tra lavoro I/O-bound e CPU-bound e lo strumento giusto per ciascuno.
Risposta di esempio in inglese da esercitare ad alta voceThe 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?
Che cos'è un decoratore e come ne scriveresti uno?
Che cosa vuole sentire l'intervistatore. Le funzioni come oggetti di prima classe, l'idea di avvolgere (wrapping), casi d'uso reali e l'abitudine di usare functools.wraps.
Risposta di esempio in inglese da esercitare ad alta voceA 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?
Come mantieni un progetto Python riproducibile e manutenibile?
Che cosa vuole sentire l'intervistatore. Ambienti virtuali, dipendenze bloccate (pinned) e il ruolo dei type hint e di un controllo statico in un team.
Risposta di esempio in inglese da esercitare ad alta voceI 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.
Come pronunciare i termini chiave
- Python
- pài-thon (il «th» è quello di «think»)
- tuple
- tàp-ol oppure tù-pol
- mutable
- miù-ta-bol
- dunder
- dàn-der (abbreviazione di «double underscore»)
- generator
- gèn-e-rèi-ter
- decorator
- dèc-o-rèi-ter
- GIL
- ghil («g» dura) oppure gi-ài-èl
- venv
- vì-ènv
Frasi utili per rispondere
- In Python, everything is an object.In Python, tutto è un oggetto.
- It depends on whether the work is I/O-bound or CPU-bound.Dipende dal fatto che il lavoro sia I/O-bound o CPU-bound.
- A tuple of immutable items can be a dictionary key.Una tupla di elementi immutabili può essere la chiave di un dizionario.
- I would use a generator here to save memory.Qui userei un generatore per risparmiare memoria.
- That is a convention in Python, not a rule of the language.Questa è una convenzione di Python, non una regola del linguaggio.
- I would check the documentation for the current behaviour.Controllerei la documentazione per il comportamento attuale.
- In practice, I would pin the dependencies.In pratica, bloccherei le versioni delle dipendenze.
Errori comuni da evitare
- Dire "Python is slow" e fermarsi lì. Spiega dove va il tempo, nella CPU o nell'attesa, e che cosa fai: prima fai il profiling, scegli la struttura dati giusta e sposti il codice critico in librerie native.
- Dire "the GIL means Python has no threads" o "threads are useless". I thread aiutano ancora con il lavoro I/O-bound. Il limite è il lavoro parallelo sulla CPU in puro Python.
- Scrivere una funzione con una lista come argomento predefinito senza conoscere il problema. Il valore predefinito viene creato una sola volta, quindi è condiviso tra le chiamate. Usa None come valore predefinito e crea la lista dentro la funzione.
- Dire "I am working with Python since five years", sul modello dell'italiano «lavoro con Python da cinque anni». Usa il present perfect con "for": "I have worked with Python for five years". Usa "since" solo con un punto di inizio, come "since 2019".
- Chiamare una lista "an array", come si fa spesso in italiano. In Python array è un tipo separato della libreria standard. Di' "list" o "sequence" così l'intervistatore capisce che cosa intendi.
Domande frequenti
Quale versione di Python devo citare?
Quella usata dal tuo ultimo progetto e una funzionalità che ti è piaciuta, per esempio i type hint, le dataclass o lo structural pattern matching nelle versioni recenti. Non vantare funzionalità di versioni che non hai usato. Dire "I would check the release notes" è meglio di una risposta tirata a indovinare.
Devo conoscere asyncio?
Per i ruoli backend e di servizi dati, conosci le basi: coroutine, event loop, await, quando aiuta (molte attese di rete) e quando no (lavoro pesante per la CPU). Un esempio reale dal tuo lavoro vale più di una lunga definizione.
Scriverò codice Python durante il colloquio?
Spesso sì: una piccola funzione o un compito sui dati. Di' prima il tuo piano, cita i casi limite come l'input vuoto e usa gli strumenti integrati, come enumerate, zip, le comprehension e il modulo collections. Alla fine di' il costo in tempo e in memoria della tua soluzione.
Leggere una risposta non è la stessa cosa che dirla. Esercita ogni domanda ad alta voce, poi confronta il significato e la lingua della tua risposta.
Prova SpeakRole ↗Altri argomenti in quest’area