我这里有一个装饰器,该装饰器将数字Public Sub TestRangeInArray()
Dim thisWB As Workbook
Dim userLoadWS As Worksheet
Set thisWB = ThisWorkbook
Set userLoadWS = thisWB.Sheets("User Load")
'--- create the range and copy into a memory array
Dim searchRange As Range
Dim searchData As Variant
Set searchRange = userLoadWS.Range("E1:E3000")
searchData = searchRange.value
Dim i As Long
For i = LBound(searchData, 1) To UBound(searchData, 1)
If Not searchData(i, 1) = vbNullString Then
searchData(i, 1) = DeleteTextAfter(searchData(i, 1), "Ext")
searchData(i, 1) = DeleteTextAfter(searchData(i, 1), "/")
End If
Next i
'--- now copy the modified array back to the worksheet range
searchRange.value = searchData
End Sub
作为输入并将其乘以函数x
的结果。完美运行的代码没有错。但是问题是,当我们使用f_(*k)
时,此修饰符如何取sq(n)
并乘以3?装饰者如何知道将函数sq(n)
的结果乘以3。因为未提供函数的参数,或者换句话说,它如何用sq(n)
代替{{1 }},因为没有将f_(*k)
作为参数。
sq(n)
答案 0 :(得分:2)
@decorator(arg)
只是说func = decorator(arg)(func)
将参数传递给装饰器时,您将返回一个全新的函数(multiply_x
)作为装饰器multiply
的返回值,并用其替换标识符sq
您刚刚返回的该函数然后采用原始函数sq
,现在该函数成为内部函数内部的f
参数。
现在,当您执行sq(n)
时,实际上是调用内部f_(n)
。此函数返回x
(您作为装饰器的参数传入的3)乘以调用函数f
(原来的sq
函数)的结果。
我希望这可以使事情变得清晰