Python
217 bites tagged Python — interview questions with model answers, and 60-second explainers.
Accessing Python Type Annotations Safely
Accessing an object's type hints isn't just `obj.__annotations__`. Use `inspect.get_annotations()` in Python 3.10+ for safe access. This is key for tools like FastAPI that introspect your code.
Python's async/await: Concurrent, Not Parallel
async/await lets a single Python thread juggle multiple tasks, pausing one to work on another while it waits for I/O. It's ideal for network requests or database queries. The footgun: it won't speed up CPU-bound tasks, it only helps with waiting.
Python Coroutines: Functions You Can Pause and Resume
A Python coroutine is a function that can be paused and resumed. It yields control during I/O waits, allowing other tasks to run instead of blocking the program. The main footgun: calling an `async` function does nothing; you must `await` it to run it.
The `with` Statement: Python's Automatic Cleanup Crew
A context manager is Python's automatic cleanup crew. It uses the `with` statement to guarantee setup and teardown code runs, even if errors occur. It's essential for files and database connections.
Python's `yield`: Functions That Pause and Resume
Python's `yield` creates a generator: a pausable function that produces values on-demand, saving memory. Use it for large files or infinite sequences. The footgun: a generator is a one-time-use iterator; you can't loop over it twice.
Python Decorators: Functions that Wrap Functions
A decorator is a function that wraps another function, adding behavior without modifying the original code. They're used for caching, logging, or access control. The main footgun is forgetting that decorators run at definition time, not call time.
Python Enums: Give Names to Magic Numbers
Python's Enum gives meaningful names to "magic numbers" or strings. Use it for fixed sets of options like statuses or categories to make code self-documenting. The footgun: don't compare members to raw values; compare member to member for type safety.
Python Packages: Grouping Modules with __init__.py
A Python package is a folder of modules treated as one unit. The `__init__.py` file marks the folder as a package and can run setup code. Use it to organize large codebases.
Python Data Classes: Write Less Boilerplate
Python's @dataclass decorator writes boilerplate code like `__init__` and `__repr__` for you, turning a class with type hints into a data container. Use it for API payloads or simple records.
Python Type Hints: Documentation Your Linter Can Read
Type hints are labels for variables and function returns (`name: str`) that Python ignores at runtime. They enable static analysis tools and IDEs to catch errors before you run code.
BentoML: Packaging Models for Production APIs
BentoML is a standardized shipping container for your ML models, packaging them into production-ready API endpoints. Use it to deploy LLMs or RAG systems without managing complex infrastructure. Its focus is purely on inference, not model training.
Conda Environments: Isolate Your Project Dependencies
Think of a Conda environment as a separate workshop for each project, with its own tools (packages) and Python version. This prevents dependency conflicts when Project A needs a different library version than Project B.
Python Virtual Environments: Isolate Project Dependencies
A Python virtual environment is a self-contained directory with its own Python interpreter and packages, preventing dependency conflicts between projects. The biggest mistake is checking the environment folder into source control; it's disposable and meant to…
Hydra: Composable Configuration for Complex Apps
Hydra treats configuration like LEGOs. Instead of one monolithic file, you compose small, reusable config pieces for each run. It's ideal for ML experiments where you override settings from the command line.
Cython: Static Typing for Faster Python
Cython speeds up Python by compiling it to C, especially when you add static types to bypass Python's dynamic overhead. Use it for CPU-bound bottlenecks like tight loops in numerical code.
Facet Grid: A Visual GROUP BY for Your Data
A Facet Grid is a visual GROUP BY. It creates a matrix of plots, each showing a different subset of your data, to compare relationships across categories. The footgun is forgetting to call `.map()` to draw the plots; the grid is empty on its own.
R & Python Interoperability with Reticulate
Reticulate embeds a Python session inside R, letting you use Python libraries as if they were native R objects. Use it when a team uses both languages or you need a Python library in an R workflow.
Dask: Parallel Computing with Familiar APIs
Dask parallelizes Python analytics by breaking data into chunks and building a task graph of operations. It's like giving Pandas and NumPy superpowers for data too big for RAM. The footgun: its lazy evaluation means you must explicitly call `.compute()`.
Scikit-learn's Universal API: Fit, Predict, Transform
The scikit-learn Estimator API is a universal contract: `.fit()` to learn, `.predict()` to guess, and `.transform()` to change data. It's used for everything from `StandardScaler` to `RandomForestClassifier`.
Matplotlib's Object-Oriented API: Explicit Plot Control
Instead of the stateful `plt.plot()`, Matplotlib's OO API gives you explicit control by creating `Figure` and `Axes` objects to call methods on, like `ax.plot()`. This is crucial for complex plots with multiple subplots. The footgun is mixing styles.
pandas DataFrame: A Spreadsheet in Code
Think of a pandas DataFrame as a powerful spreadsheet you control with code. It's the workhorse for loading, cleaning, and analyzing tabular data in Python, like sales figures from a CSV.
NumPy ndarray: Fast, Typed, Multidimensional Grids
A NumPy ndarray is a fast, memory-efficient grid for numbers of a single type. It's the backbone for scientific computing, used for image data to ML model weights. The main footgun: slicing often creates a view, not a copy, so edits can alter the original.
Apache Airflow: Code-Defined Data Pipelines
Airflow lets you define, schedule, and monitor complex data workflows as code, replacing brittle cron jobs. It's used for ETL jobs or ML training pipelines. The footgun is treating it as a data processing engine; it's an orchestrator, not the worker.
Jupyter Notebooks: Interactive Code Sandboxes
Jupyter Notebooks are digital lab notebooks for running code, seeing output, and writing notes in one place. Data scientists use them for exploration, visualization, and prototyping.
Get Python bites daily.
Five a day, five minutes, offline. With quizzes so it sticks.
Open testing — you’ll join as an early tester.