Original title:
"Help me understand this weird Python idiom?
sys.stdout = codecs.getwriter(\'utf-8\')(sys.stdout)
"
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)
.getwriter
returns a functioncallable object; you are merely calling it in the same line.
Example:
def returnFunction():
def myFunction():
print('hello!')
return myFunction
Demo:
>>> returnFunction()()
hello!
You could have alternatively done:
>>> result = returnFunction()
>>> result()
hello!
Visualization:
evaluation step 0: returnSomeFunction()()
evaluation step 1: |<-somefunction>-->|()
evaluation step 2: |<----result-------->|
codecs.getwriter('utf-8')
returns a class with StreamWriter
behaviour and whose objects can be initialized with a stream.
>>> codecs.getwriter('utf-8')
<class encodings.utf_8.StreamWriter at 0x1004b28f0>
Thus, you are doing something similar to:
sys.stdout = StreamWriter(sys.stdout)