DHI / DHI/python-package-development

Clarify benefits of factory methods in Module 2

Offen
#31 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Jupyter Notebook
Sterne
8
Forks
1
Ø Merge
4 Min.
Gemergte PRs (30 T.)
1

Beschreibung

The static methods section in Module 2 shows *how* to create factory methods but could better explain *when and why* to use them.

## Current state
The notes mention it's "less cumbersome" to create instances, but the pedagogical value could be stronger.

## Suggested improvements

1. **Name the pattern** - Use the term "factory method" or "alternative constructor" so students can recognize and search for it

2. **Explain the "why"** - Python doesn't support multiple `__init__` signatures, so factory methods provide multiple construction paths with semantic clarity

3. **Add stdlib examples from datetime**:
- `datetime.fromtimestamp()`
- `datetime.fromisoformat()`
- `date.fromordinal()`

4. **Add examples from polars**:
- `pl.read_csv()`, `pl.read_parquet()`, `pl.read_json()` (module-level factories)
- `DataFrame.to_dict()` → `pl.from_dict()` round-trip
- `pl.from_numpy()`, `pl.from_pandas()`

These examples show the pattern is pervasive in well-designed Python APIs.

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.