解释 probSecond.calls 如何等于零
Explain how probSecond.calls equal to zero
def MainCount(f):
def progFirst(*args,**kwargs):
progFirst.calls+=1
return f(*args,**kwargs)
progFirst.calls=0
return progFirst
@MainCount
def progSecond(i):
return i+1
@MainCount
def Count(i=0,j=1):
return i*j+1
print(progSecond.calls)
for n in range(5):
progSecond(n)
Count(j=0,i=1)
print(Count.calls)
输出:0
1
根据我的理解 MainCount(probSecond) 但我不明白 probSecond.calls 如何在 Count.calls 中也等于零
正如您在 MainCount 函数中看到的那样 probFirst.Calls 是函数的属性。当 MainCount(probSecond) 现在 probSecond.calls 也是 MainCount 函数的属性。
# A Python example to demonstrate that
# decorators can be useful attach data
# A decorator function to attach
# data to func
def attach_data(func):
func.data = 3
return func
@attach_data
def add (x, y):
return x + y
# Driver code
# This call is equivalent to attach_data()
# with add() as parameter
print(add(2, 3))
print(add.data)
def MainCount(f):
def progFirst(*args,**kwargs):
progFirst.calls+=1
return f(*args,**kwargs)
progFirst.calls=0
return progFirst
@MainCount
def progSecond(i):
return i+1
@MainCount
def Count(i=0,j=1):
return i*j+1
print(progSecond.calls)
for n in range(5):
progSecond(n)
Count(j=0,i=1)
print(Count.calls)
输出:0 1
根据我的理解 MainCount(probSecond) 但我不明白 probSecond.calls 如何在 Count.calls 中也等于零
正如您在 MainCount 函数中看到的那样 probFirst.Calls 是函数的属性。当 MainCount(probSecond) 现在 probSecond.calls 也是 MainCount 函数的属性。
# A Python example to demonstrate that
# decorators can be useful attach data
# A decorator function to attach
# data to func
def attach_data(func):
func.data = 3
return func
@attach_data
def add (x, y):
return x + y
# Driver code
# This call is equivalent to attach_data()
# with add() as parameter
print(add(2, 3))
print(add.data)