Python code snippets
Snippets collection from friendly snippets.
Basic snippets
- Shebang line:
#env→#!/usr/bin/env python - Pyright ignore line:
#ignore→# pyright: ignore[] - Multiline string:
#→ multiline block - Self reference:
s→self. - Magic methods:
__→__init__(customizable) - Basic class:
class→class classname: - Derived class:
classd→class classname(parent): - Class template:
classi→ Full class withdocstring, __init__, and super()
Functions & Methods
- Function:
def→def fname(): - Function with return type:
deft→def fname() -> None: - Method:
defs→def mname(self): - Method with return type:
defst→def mname(self) -> None: - Property template:
property→ Full @property decorator template - Lambda:
lambda→lambda parameter_list: expression
Control Flow
- If statement:
if→if condition: - If/else:
ifelse→ Completeif/elsestructure - Match/case:
match→match expression:with case block - Case wildcard:
casew→case _:
Loops & Iteration
- For loop:
for→for value in iterable: - For with range:
forr→for value in range(): - While loop:
while→while condition: - With statement:
with→with expression as target:
Error Handling
- Try/except:
try→ Completetry/exceptwith exception handling - Try/except/else:
trya→try/except/elseblocks - Try/except/finally:
tryf→try/except/finallyblocks - Try/except/else/finally:
tryef→try/except/else/finallyblocks - Except block:
except→except: - Except as:
exceptas→except Exception as e:
Advanced
Import: import → import datetime
From import: fromim → from pathlib import Path
Main guard: ifmain → if __name__ == "__main__":
Jupyter cell: #cell → # [markdown]