像這樣?
def weight(w):
def result(n):
print(n * w)
return result
f1 = weight(2)
f2 = weight(3)
f3 = weight(4)
f1(10)
f2(35)
f3(67)
與 JS 用法一模一樣,Lambda 也可以做到。
import numpy as np
def test():
weights = np.array([2,3,4])
def inner(data):
print(data * weights)
return inner
obj = test()
obj(np.array([10,35,67]))
def weight(*nums):
def inner(*weights):
return [n*w for n, w in zip(nums, weights)]
return inner
weight(10, 35, 67)(2, 3, 4)