What is the idiomatic Python equivalent of this C/C++ code?
void foo() { static int counter = 0; counter++; printf('counter is %d\n', counter); }
specifically, how does one implement the static member at the function level, as opposed to the class level? And does placing the function into a class change anything?
A bit reversed, but this should work:
If you want the counter initialization code at the top instead of the bottom, you can create a decorator:
Then use the code like this:
It’ll still require you to use the
foo.prefix, unfortunately.(Credit: @ony)