02open 25 min

Syntax and structure: from modules and impl blocks to Python's simpler execution model

Map Rust modules, functions, and type-associated behavior onto Python modules, functions, and classes.

by the end of this lesson you can

  • Uses a Python class or function shape intentionally
  • Keeps behavior obvious without Rust-specific ceremony
  • Lets the Python execution model stay simple

Overview

Rust developers already understand structured code organization, but Python presents it with fewer language-level distinctions. There are modules, functions, classes, and imports, but there is less pressure to encode relationships through syntax like impl, visibility modifiers, or associated functions unless the design truly benefits from it.

In Rust, you often

organize code with modules, structs, enums, traits, and impl blocks that make relationships explicit.

In Python, the common pattern is

to organize code with modules, functions, and classes while keeping the execution model and file structure simpler and more direct.

why this difference matters

This lesson makes Python feel legible to a Rust developer without pretending it has the same structural vocabulary.

Rust

struct User {
    name: String,
}

impl User {
    fn greet(&self) -> String {
        format!("hi {}", self.name)
    }
}

Python

class User:
    def __init__(self, name: str):
        self.name = name

    def greet(self) -> str:
        return f"hi {self.name}"

Deeper comparison

Rust version

mod config {
    pub fn load() -> Result<Settings, ConfigError> {
        Ok(Settings { debug: false })
    }
}

Python version

def load_config():
    return {"debug": False}

Reflect

What gets easier once you stop looking for Rust's module and associated-function patterns inside Python code?

what a strong answer notices

A strong answer mentions simpler scanning, less structural ceremony, and a better sense of when Python wants a plain function instead of a more formal type-centered design.

Rewrite

Rewrite this Rust struct-and-impl shape into Python and choose the simplest clear structure.

Rewrite this Rust

struct Counter {
    value: i32,
}

impl Counter {
    fn inc(&mut self) {
        self.value += 1;
    }
}

what good looks like

  • Uses a Python class or function shape intentionally
  • Keeps behavior obvious without Rust-specific ceremony
  • Lets the Python execution model stay simple

Practice

Sketch a Python module for request validation starting from a Rust mental model of module plus impl.

success criteria

  • Uses modules, functions, and classes intentionally
  • Avoids forcing Rust structure where Python would be flatter
  • Explains what belongs at module scope versus inside a class

Common mistakes

  • Looking for impl-style grouping everywhere in Python.
  • Forcing class-heavy structure when plain functions would be clearer.
  • Expecting Python module boundaries to advertise relationships as explicitly as Rust does.

takeaways

  • This lesson makes Python feel legible to a Rust developer without pretending it has the same structural vocabulary.
  • A strong answer mentions simpler scanning, less structural ceremony, and a better sense of when Python wants a plain function instead of a more formal type-centered design.
  • Uses modules, functions, and classes intentionally