Published on

Python Interview Notes(Part-1)

Authors

Module & Package

  • Module: A single file containing Python code (my_module.py).
  • Package: A collection of modules organized in directories, with an __init__.py file (my_package directory containing multiple modules).

Single underscore variable

The single underscore (_) in Python serves multiple purposes, including acting as a placeholder for temporary or ignored variables, holding the result of the last evaluated expression in the interactive interpreter, and serving as a convention for translation functions in internationalization. Understanding these uses helps write cleaner, more readable, and idiomatic Python code.

Class Methods

  • Decorator: @classmethod
  • First Parameter: cls (the class itself)
  • Usage: Used to access or modify class state. Can be used for factory methods to create instances.
  • Access: Can access and modify class-level state.

Static Methods

  • Decorator: @staticmethod
  • First Parameter: None (acts like a regular function within the class's namespace)
  • Usage: Used for utility functions that don't need access to class or instance state.
  • Access: Cannot access or modify class-level or instance-level state.

Variable sharing in python

  • Mutable Types: When objects contain references to mutable types, changes to the mutable object will be visible to all references.
  • Class Variables: Class variables are shared among all instances of the class. Changes to a class variable affect all instances.
  • Mutable Types: Lists, dictionaries, sets, byte arrays.
  • Immutable Types: Integers, floats, strings, tuples, frozen sets, bytes.

Method Resolution Order

  • MRO (Method Resolution Order): The order in which Python looks for methods in the class hierarchy.
  • C3 Linearization: The algorithm used by Python 3 to determine the MRO, ensuring a consistent and predictable order.
  • Viewing MRO: Use the __mro__ attribute or the mro() method to view the MRO of a class.

Garbage Collection

  • Reference Counting: Each object has a reference count. When the count drops to zero, the object is reclaimed.
  • Cyclic Garbage Collection: Detects and collects cyclic references that reference counting can't handle.
  • Generational Garbage Collection: Objects are divided into generations to optimize collection frequency.
  • Control and Tuning: The gc module allows manual control and tuning of garbage collection behavior.

Writing code that help GC

  1. Avoid Circular References:
    • Circular references between objects prevent them from being garbage collected. Break circular references when possible or use weak references.
  2. Clear References When Not Needed:
    • Ensure references to objects are cleared (None) or go out of scope when they are no longer needed, especially for large or memory-intensive objects.
  3. Use Context Managers (with Statements):
    • Context managers automatically clean up resources (like file handles) after their block of code is executed, helping manage memory effectively.
  4. Manually Trigger Garbage Collection:
    • Occasionally trigger garbage collection (gc.collect()) in long-running processes to manage memory fragmentation and ensure efficient memory usage.
  5. Avoid Memory Leaks:
    • Ensure that objects are properly deallocated and references are managed to avoid memory leaks, where memory is allocated but never released.
  6. Profile and Optimize Memory Usage:
    • Use profiling tools to identify and optimize memory-intensive parts of your code, optimizing data structures and minimizing unnecessary object creation.