Python as C++'s limiting case

September 30, 2023

The Python language’s memory model can be deduced from first principles: simply take modern C++ conventions and drive their safety and generality to infinity.

  • syntax
    • simple control flow
    • backslashes for strings
    • iterator-based, range-based for loops
    • optional and keyword arguments
    • expression reading
    • return multiple values
      • swapping is a single line (no temporary variables)
  • readability
    • boolean logic
    • short-circuit evaluation
  • low-level access
    • tokenizers, ast, raw UNIX calls
  • namespaces
    • every module is it's own global namespace
    • don't need static
  • data structures
    • zeroes and empty data structures are false
    • eliminate integer overflow
  • type system
    • python has one type (CPython)
      • struct _typeobject
      • relevant methods to allow python's syntax
      • types belong to values (a = 3), dynamic language
    • loose typing helped keep it popular during initial days
    • locality hindered by loose typing
  • pointers
    • every variable is a pointer to an object
      • functions and types become eligible data
    • two pointer operations
      • copying
      • calling a method
        • cannot corrupt memory
        • objects are fully encapsulated
        • int, float and str types are read-only
    • nulls
      • no syntax for creating null pointer
      • segmentation faults impossibles
      • buffer overflows impossible
    • reference counting vs GC
      • locality
        • freed memory is often re-used immediately

References: