python函數后面有多個括號怎么理解?


一般而言,函數后面只有一個括號。如果看見括號后還有一個括號,說明第一個函數返回了一個函數,如果后面還有括號,說明前面那個也返回了一個函數。以此類推。

比如fun()()

def fun():
    print("this is fun");
    def _fun():
        print("this is _fun");
    return _fun;

Your task is to write a higher order function for chaining together a list of unary functions. In other words, it should return a function that does a left fold on the given functions.
chained([a,b,c,d])(input)

Should yield the same result as

d(c(b(a(input))))

def fun81(functions):
    def f(x):
        for fun in functions:
            x = fun(x);
        return x;
    return f;

小結:python中也可以鏈式點用函數,只是函數需要在返回一個函數。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM