2016-10-29 37 views
-3

我真的不明白這是如何工作的,爲什麼10不是函數的變量?我從來沒有見過一種情況,你可以將一個函數應用到該函數之外的某個項目。我不想要這個問題的答案,只是想了解它。在Python中應用帶一個參數值的函數

謝謝你們夥計

寫一個叫做general_poly的函數,它符合下面的規範。 例如,general_poly([1,2,3,4])(10)應該計算爲1234,因爲1 * 103 + 2 * 102 + 3 * 101 + 4 * 100因此在該示例中,僅函數與general_poly([1,2,3,4])一個參數,它返回一個函數,您可以應用於一個值,在這種情況下,x = 10 general_poly([1,2,3,4])(10 )。

回答

2

它要求你general_poly返回的功能,例如:

def general_poly(L): 
    def inner(x): 
     return sum(x+e for e in L) 
    return inner 

general_poly([1,2,3,4])(10) 
# 11+12+13+14 = 50 

這應該給你足夠的能夠通過你的家庭作業工作。

+0

非常感謝!我所需要的。 – Aionis

0
def general_poly (L): 
    """ L, a list of numbers (n0, n1, n2, ... nk) 
    Returns a function, which when applied to a value x, returns the value 
    n0 * x^k + n1 * x^(k-1) + ... nk * x^0 """ 

    def inner(x): 
     L.reverse() 
     return sum(e*x**L.index(e) for e in L) 
    return inner 
相關問題