Programming · free software · the open webSubscribe via RSS

A reading path

Python: object boundaries, subprocesses and reliable text data

Worked Python notes on name binding, special methods, virtual environments, safe subprocess arguments and tested CSV parsing.

Separate names, objects and interpreters

A copied binding can still point to the same mutable object. An instance attribute can differ from the special method used by a built-in operation. An activated shell can still launch a different Python interpreter. The first three notes use small experiments to make each boundary visible rather than relying on a metaphor.

Keep data out of accidental parsers

When running a command, construct an argument vector before deciding whether a shell is needed. When reading CSV, use a CSV parser before interpreting fields as numbers or application identifiers. Both boundaries fail when a familiar-looking string is mistaken for already-structured data.

Run the examples and challenge their assumptions

The CSV examples include standard-library unittest cases for embedded newlines, duplicate headers, field widths and leading-zero identifiers. Download a file and run it with Python 3.10 or newer. The object-model notes show expected output; the virtual-environment example reports the interpreter that actually ran it.

Work through the notes

  1. A Python name is a binding, not a box containing a copy

    Python assignment binds names to objects, so mutating a shared list is visible through every alias while rebinding one name changes only that binding.

    Download the accompanying example
  2. Why len(x) is not just x.__len__() in disguise

    Implicit operations such as len use a type-level special method protocol, which differs from ordinary attribute lookup on an instance.

    Download the accompanying example
  3. A virtual environment has an interpreter boundary

    A Python virtual environment is selected by the interpreter that runs your program. Learn how pyvenv.cfg, interpreter prefixes, shell activation, and python -m pip reveal which environment owns installed packages.

    Download the accompanying example
  4. A command line is an argument vector before it is a shell string

    Passing subprocess arguments as a sequence preserves argument boundaries and avoids shell parsing by default. It prevents shell metacharacters from becoming syntax, while leaving program-specific option handling as a separate concern.

    Download the accompanying example
  5. CSV round trips: commas, quotes and embedded newlines

    Use Python’s CSV reader and writer to round-trip commas, quotes and newlines while accounting for type loss and spreadsheet formula risks.

    Download the accompanying example
  6. Validate CSV headers before building dictionaries

    Check CSV header uniqueness, required names and row widths before mapping fields, preserving IDs such as 007 as strings.

    Download the accompanying example

Primary documentation

Historical articles