1. 程式人生 > >python函數後面有多個括號怎麽理解?

python函數後面有多個括號怎麽理解?

python函數 pytho eth left hat functions lis task order

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

比如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中也可以鏈式點用函數,只是函數需要在返回一個函數。

python函數後面有多個括號怎麽理解?