没有数.一次调用一个函数?

use*_*303 0 python

是否有任何方法可以计算在python中调用函数的次数?我在GUI中使用了checkbutton.我已经为该checkbutton命令编写了一个函数,我需要根据checkbutton状态执行一些操作,我的意思是根据它是否被勾选.我的检查按钮和按钮语法是这样的

All = Checkbutton (text='All', command=Get_File_Name2,padx =48, justify = LEFT)
submit = Button (text='submit', command=execute_File,padx =48, justify = LEFT)
Run Code Online (Sandbox Code Playgroud)

所以我认为没有.调用命令函数的次数,并根据其值,我可以决定是否勾选.请帮忙

Vla*_*nov 13

您可以编写将在函数调用后递增特殊变量的装饰器:

from functools import wraps

def counter(func):
    @wraps(func)
    def tmp(*args, **kwargs):
        tmp.count += 1
        return func(*args, **kwargs)
    tmp.count = 0
    return tmp

@counter
def foo():
    print 'foo'

@counter
def bar():
    print 'bar'

print foo.count, bar.count  # (0, 0)
foo()
print foo.count, bar.count  # (1, 0)
foo()
bar()
print foo.count, bar.count  # (2, 1)
Run Code Online (Sandbox Code Playgroud)