Educational collection of surprising Python code snippets that demonstrate counter-intuitive behaviors and language internals
npx @tessl/cli install tessl/pypi-wtfpython@3.0.0An educational collection of surprising Python code snippets that demonstrate counter-intuitive behaviors and language internals. wtfpython helps developers understand Python's quirks, edge cases, and the reasons behind unexpected behaviors through carefully curated examples with detailed explanations.
pip install wtfpythonIMPORTANT: This package provides no importable API. It is purely an educational resource consisting of documentation and example code snippets.
No imports available - wtfpython cannot be imported as a module:
# This will NOT work:
import wtfpython # ModuleNotFoundError
from wtfpython import anything # ModuleNotFoundErrorwtfpython is designed to be:
# Install the package
pip install wtfpython
# Find installation location
python -c "import pkg_resources; print(pkg_resources.get_distribution('wtfpython').location)"
# Navigate to the installed package to read examples
# The main content is in README.md (125KB+ of examples and explanations)The package includes standalone Python files demonstrating specific behaviors:
# Run string behavior examples directly
python -c "
import pkg_resources
import os
import subprocess
pkg_path = pkg_resources.get_distribution('wtfpython').location
subprocess.run(['python', os.path.join(pkg_path, 'wtfpython', 'snippets', '2_tricky_strings.py')])
"
# Or navigate to package directory and run files
cd $(python -c "import pkg_resources; print(pkg_resources.get_distribution('wtfpython').location)")/wtfpython
python snippets/2_tricky_strings.py
python mixed_tabs_and_spaces.py
# Run validation tests using nox (if nox is installed)
nox -s tests
# Tests run across Python versions: 3.9, 3.10, 3.11, 3.12, 3.13wtfpython is structured as an educational content package:
The package serves as a reference guide for understanding Python's internal mechanics, object model, and interpreter behaviors that can surprise developers.
The main educational content covers various Python behaviors and internals, organized into sections that demonstrate surprising or counter-intuitive language features.
# No programmatic API - content is accessed by reading files
# Key content areas include:
# - String interning and identity behaviors
# - Variable scoping and closure edge cases
# - Boolean and comparison quirks
# - Mutable default arguments
# - Generator and iterator surprises
# - Class and inheritance behaviors
# - Numeric type behaviors
# - Exception handling edge casesContent Access Pattern:
Standalone Python files that can be executed to demonstrate specific behaviors.
# Files available for execution:
# - snippets/2_tricky_strings.py: String identity and interning examples
# - mixed_tabs_and_spaces.py: Mixed indentation demonstration
# - noxfile.py: Test runner configuration
# Executable functions available in package files:
def square(x):
"""
Demonstrates mixed tab/space indentation issue in Python.
Located in: mixed_tabs_and_spaces.py
Args:
x (int): Number to square
Returns:
int: Square of x (calculated via repeated addition)
Note: This function intentionally contains mixed indentation
that will raise TabError in Python 3
"""
@nox.session(python=["3.9", "3.10", "3.11", "3.12", "3.13"], reuse_venv=True)
def tests(session):
"""
nox session for running wtfpython example validation.
Located in: noxfile.py
Args:
session: nox session object
Returns:
None
Runs:
python snippets/2_tricky_strings.py
"""Usage Pattern:
# After installation, find and execute example files
python path/to/wtfpython/snippets/2_tricky_strings.py
# Run mixed indentation demonstration
python path/to/wtfpython/mixed_tabs_and_spaces.py
# Run tests using nox
nox -s testsAdditional functions available in development/utility files (not part of main educational content):
def generate_random_id_comment():
"""
Generate random UUID comment for README sections.
Located in: irrelevant/insert_ids.py
Returns:
str: UUID comment string for HTML insertion
"""
# notebook_generator.py contains functions for:
# - Converting README.md to Jupyter notebook format
# - Processing example sections into notebook cells
# - Handling code extraction and formatting
# obsolete/parse_readme.py contains legacy functions for:
# - Parsing original README format
# - Categorizing examples
# - Reordering content structure
# obsolete/generate_contributions.py contains functions for:
# - Parsing contributor information from README
# - Generating CONTRIBUTORS.md table
# - Processing GitHub issue referenceswtfpython/
├── README.md # Main educational content (125KB+ examples)
├── pyproject.toml # Package metadata and dependencies
├── noxfile.py # Test configuration (nox sessions)
├── mixed_tabs_and_spaces.py # Mixed indentation demonstration
├── snippets/
│ ├── __init__.py # Empty file (no imports)
│ └── 2_tricky_strings.py # String behavior examples
├── irrelevant/ # Utility and development files
│ ├── insert_ids.py # UUID insertion utility
│ ├── notebook_generator.py # Jupyter notebook generator
│ └── obsolete/ # Legacy utilities
│ ├── parse_readme.py # README parsing utility
│ └── generate_contributions.py # Contribution generator
├── images/ # Logo and visual assets
├── translations/ # Multi-language versions
├── .github/ # GitHub configuration
├── .gitignore # Git ignore rules
├── .markdownlint.yaml # Markdown linting config
├── .pre-commit-config.yaml # Pre-commit hooks
├── .travis.yml # CI configuration
├── LICENSE # WTFPL 2.0 license
├── CONTRIBUTING.md # Contribution guidelines
├── CONTRIBUTORS.md # Contributor list
└── code-of-conduct.md # Code of conductwtfpython serves as: