What is the idiomatic Python equivalent of this C/C++ code?
void foo() { static int counter = 0; counter++;
Many people have already suggested testing 'hasattr', but there's a simpler answer:
def func(): func.counter = getattr(func, 'counter', 0) + 1
No try/except, no testing hasattr, just getattr with a default.