liveBook · Manning

Python

As explained in Refactoring to Rust

Python is a high-level general-purpose programming language commonly implemented with automatic memory management and garbage collection.

Python is a high-level, general-purpose language valued for readable syntax, flexibility, and rapid development. It is often used for prototypes, research, machine learning, data mining, and production systems, but compute-heavy services may require performance-oriented refactoring. section 8.0 section 1.4.1

Python’s practical characteristics include:

  • Uses indentation to provide structure and runs through an interpreter, enabling quick feedback without a compilation step. section 8.0
  • Leaves types implicit in ordinary function syntax, while operations still expect compatible values and can raise exceptions when inputs are invalid. section 1.4.1 section 8.0
  • Relies on runtime allocation and automatic memory management, which can increase resource use in allocation-heavy processing. section 1.4.1
  • Connects to native ecosystems through implementations such as CPython and libraries written in C or C++. section 8.0

Summing a CSV column

A small function can split CSV input into lines and fields, convert each selected value to an integer, and accumulate the result. This straightforward implementation illustrates Python’s concise syntax but also allocates lists for lines and rows. A measured comparison reports 2.9 seconds and 800 MiB for Python, versus 146 milliseconds and 350 MiB for an equivalent Rust implementation. section 1.4.1

Listing 1.1 Python function: The sum of values from a column in a CSV string
def sum_csv_column(data, column):
  sum = 0

  for line in data.split("\n"):
    if len(line) == 0:
      continue

    value_str = line.split(",")[column]
    sum += int(value_str)

  return sum

Inspect the complete Python CSV-column aggregation function, including input splitting, conversion, accumulation, and return behavior. section 1.4.1

How Refactoring to Rust covers it

How deep it goes
Explains syntax, interpretation, typing, allocation, native integration, and one measured performance comparison, but defers full runtime internals.
Examples
Uses CSV column aggregation, Python extension modules, and computationally expensive asynchronous applications as concrete examples.
What you can do after
Evaluate Python bottlenecks with benchmarking and profiling, then apply Rust integration or extensions to performance-critical paths.
How it connects
Connects Python with Rust, C, C++, benchmarking, profiling, threads, asynchronous programming, and extension modules.

Also explained in

Compare how 26 books explain Python →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Refactoring to Rust. The section links point to the parts of the book it draws on.