What do double parentheses mean in a function call? e.g. func(stuff)(stuff)?

后端 未结 3 1038
悲哀的现实
悲哀的现实 2020-12-30 01:19

Original title:

"Help me understand this weird Python idiom? sys.stdout = codecs.getwriter(\'utf-8\')(sys.stdout)"

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-30 01:24

    Calling the wrapper function with the double parentheses of python flexibility .

    Example

    1- funcWrapper

    def funcwrapper(y):
        def abc(x):
            return x * y + 1
        return abc
    
    result = funcwrapper(3)(5)
    print(result)
    

    2- funcWrapper

    def xyz(z):
        return z + 1
    
    def funcwrapper(y):
        def abc(x):
            return x * y + 1
        return abc
    
    result = funcwrapper(3)(xyz(4))
    print(result)
    

提交回复
热议问题