このコードから得られるのは、Python の print はwrite
method ofのラッパー関数であるstdout
ため、戻り値の型を指定すると、それも返さなければならないということだけです。では、なぜそれができないのでしょうか。
import sys
class CustomPrint():
def __init__(self):
self.old_stdout=sys.stdout
def write(self, text):
text = text.rstrip()
if len(text) == 0: return
self.old_stdout.write('custom Print--->' + text + '\n')
return text
sys.stdout=CustomPrint()
print "ab" //works
a=print "ab" //error! but why?