Python equivalent of golang’s defer statement
To emulate defer fmt.Println(*a, i) example, you could use contextlib.ExitStack: #!/usr/bin/env python3 from contextlib import ExitStack from functools import partial print(“counting”) with ExitStack() as stack: for i in range(10): a = i stack.callback(partial(print, a, i)) x = 42 a = x print(“done”) Output counting done 9 9 8 8 7 7 6 6 5 5 … Read more