Q:

static variable in a function with Python Decorator

I was looking for about static value in Python.
And I found this.
def static_var(varname, value):
    def decorate(func):
        setattr(func, varname, value)
        return func
    return decorate

@static_var("counter", 0)
def foo():
    foo.counter += 1
    print "Counter is %d" % foo.counter

It is using python decorator for static variable in a function.
The decorator(static_var) initialize static value(foo.counter) before returning the function(foo) decorated.
So I think it should not work as expected, because decorator(static_var) initialize foo.counter every time when foo is called.
As a result, I think if foo() is called two times, it should print 1 two times  
foo()     
foo()     

But It prints 1 and 2, increasing foo.counter
Why...?
Why isn't foo.counter initialized to 0 every time when foo is called?

A:

Because:
def static_var(varname, value):
    def decorate(func):
        setattr(func, varname, value)
        return func
    return decorate

@static_var("counter", 0)
def foo():
    foo.counter += 1
    print "Counter is %d" % foo.counter

is equivalent to:
foo = static_var("counter", 0)(foo)

Notice that static_var() actually gets called? Now walk through your decorator; in fact step through it with import pdb; pdb.set_trace()

Decorators wrap other functions potentially either mutating them or
  returning new functions or both.

See: Understanding Python Decorators in 12 easy steps
If we stick a few print(s) in your decorator watch what happens:
def static_var(varname, value):
    print "1"

    def decorate(func):
        print "2"
        setattr(func, varname, value)
        return func
    print "3"
    return decorate

@static_var("counter", 0)
def foo():
    foo.counter += 1
    print "Counter is %d" % foo.counter

foo()
foo()

Output:
$ python foo.py
1
3
2
Counter is 1
Counter is 2

So as I said above; firstly static_var("counter", 0) is called;
  then the return of that (which returns a function) is called with
  foo as it's first argument which sets up the initial "counter" and
  returns the same argument (the function foo).

