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: sself.
  • Magic methods: ____init__ (customizable)
  • Basic class: classclass classname:
  • Derived class: classdclass classname(parent):
  • Class template: classi → Full class with docstring, __init__, and super()

Functions & Methods

  • Function: defdef fname():
  • Function with return type: deftdef fname() -> None:
  • Method: defsdef mname(self):
  • Method with return type: defstdef mname(self) -> None:
  • Property template: property → Full @property decorator template
  • Lambda: lambdalambda parameter_list: expression

Control Flow

  • If statement: ifif condition:
  • If/else: ifelse → Complete if/else structure
  • Match/case: matchmatch expression: with case block
  • Case wildcard: casewcase _:

Loops & Iteration

  • For loop: forfor value in iterable:
  • For with range: forrfor value in range():
  • While loop: whilewhile condition:
  • With statement: withwith expression as target:

Error Handling

  • Try/except: try → Complete try/except with exception handling
  • Try/except/else: tryatry/except/else blocks
  • Try/except/finally: tryftry/except/finally blocks
  • Try/except/else/finally: tryeftry/except/else/finally blocks
  • Except block: exceptexcept:
  • Except as: exceptasexcept Exception as e:

Advanced

Import: importimport datetime From import: fromimfrom pathlib import Path Main guard: ifmainif __name__ == "__main__": Jupyter cell: #cell# [markdown]