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

Viewed 6804

I use this idiom all the time to print a bunch of content to standard out in utf-8 in Python 2:

sys.stdout = codecs.getwriter('utf-8')(sys.stdout)

But to be honest, I have no idea what the (sys.stdout) is doing. It sort of reminds me of a Javascript closure or something. But I don't know how to look up this idiom in the Python docs.

Can any of you fine folks explain what's going on here? Thanks!

3 Answers

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)
Related