Static member of a function in Python ?

Possible Duplicate:
Static class variables in Python
What is the Python equivalent of static variables inside a function?

How can I use static fields in Python ?

for example i want to count how many times the function has been called - how can i do this ?


If you wish to count how many times a method has been called, no matter which instance called it, you could use a class member like this:

class Foo(object):
    calls=0            # <--- call is a class member
    def baz(self):
        Foo.calls+=1

foo=Foo()
bar=Foo()
for i in range(100): 
    foo.baz()
    bar.baz()
print('Foo.baz was called {n} times'.format(n=foo.calls))
# Foo.baz was called 200 times

When you define calls this way:

class Foo(object):
    calls=0            

Python places the key-value pair ('calls', 0) in Foo.__dict__ .

It can be accessed with Foo.calls . Instances of Foo , such as foo=Foo() , can access it with foo.calls as well.

To assign new values to Foo.calls you must use Foo.calls = ... . Instances can not use foo.calls = ... because that causes Python to place a new and different key-value pair in foo.__dict__ , where instance members are kept.


Here is some example counting the number of calls of all objects of the same class:

class Swallow():
    i = 0 # will be used for counting calls of fly()
    def fly(self):
        Swallow.i += 1

And this is the proof:

>>> a = Swallow()
>>> b = Swallow()
>>> a.fly()
>>> a.i
1
>>> Swallow.i
1
>>> b.fly()
>>> b.i
2
>>> Swallow.i
2

so you can read it by giving the object name or class name.


Here's a decorator adding counting to a function.

import functools

def count_calls(func):
    @functools.wraps(func)
    def decor(*args, **kwargs):
        decor.count += 1
        return func(*args, **kwargs)
    decor.count = 0
    return decor

Usage:

>>> @count_calls
... def foo():
...     pass
...
>>> foo.count
0
>>> foo()
>>> foo.count
1
链接地址: http://www.djcxy.com/p/78764.html

上一篇: 包含一组对象的Python对象很奇怪

下一篇: Python中的一个函数的静态成员?