1. Senior

    What is the relationship between __eq__ and __hash__ in Python?

    If two objects are equal according to __eq__, they must have the same hash. Defining __eq__ alone makes Python set __hash__ to None, so the object becomes unhashable, and fields that feed the hash must not change while the object is a dict key or set element.

    Read the full explanation
  2. Mid-level

    What is the difference between a shallow copy and a deep copy?

    A shallow copy creates a new container whose inner elements are the same objects, while a deep copy recursively copies the whole object graph, handling circular references.

    Read the full explanation
  3. Junior

    What is the difference between an iterable and an iterator?

    An iterable is an object you can get an iterator from through iter() (such as a list); an iterator remembers its position, returns the next item through next(), and is consumed only once.

    Read the full explanation
  4. Mid-level

    How do generators and yield reduce memory usage?

    A generator produces values on demand, one at a time, instead of building a full collection, so memory stays roughly constant no matter how large the data is.

    Read the full explanation
  5. Mid-level

    Which protocol makes an object usable in a for loop?

    The object must define __iter__ returning an iterator, and the iterator defines __next__ that returns the next item and raises StopIteration at the end (and defines __iter__ returning self).

    Read the full explanation
  6. Mid-level

    When should you use a property instead of explicit getter and setter methods?

    A property exposes a computed or validated value through plain attribute syntax, and you can start with a simple attribute and turn it into a property later without breaking the interface.

    Read the full explanation
  7. Junior

    What does the @dataclass decorator give you?

    It automatically generates __init__, __repr__ and __eq__ (and optionally ordering, hash and freezing) from the fields declared with type annotations.

    Read the full explanation
  8. Senior

    What is __slots__ for, and what trade-offs come with it?

    __slots__ declares a fixed list of attributes and removes the per-instance __dict__, which reduces memory and speeds up access slightly, but it prevents adding dynamic attributes and complicates inheritance.

    Read the full explanation
  9. Senior

    How do MRO and super() work with multiple inheritance?

    The MRO (computed with the C3 algorithm) determines the order in which classes are searched for methods, and super() does not necessarily call the direct parent but the next class in the MRO of the actual object.

    Read the full explanation
  10. Mid-level

    Which protocol does a context manager implement?

    The object defines __enter__ (for setup and returning the value bound by as) and __exit__ (for cleanup), or it is built easily with contextlib.contextmanager, and with guarantees the cleanup runs even if an exception occurs.

    Read the full explanation