如果我有一個包含大量打印語句的函數:Python封裝函數以打印到變量
ie。
def funA():
print "Hi"
print "There"
print "Friend"
print "!"
我想要做的就是這樣的事情
def main():
##funA() does not print to screen here
a = getPrint(funA()) ##where getPrint is some made up function/object
print a ##prints what funA would normally print at this step
所以當FuncA的被稱爲它不會做任何印刷,而是輸出到一個目標。然後我打印對象以獲得結果。有沒有辦法做到這一點?我也不想觸摸原始功能。
我希望它是有道理的。
相關:http:// stackoverflow。com/questions/1218933/can-i-redirect-the-stdout-in-python-into-some-sort-of-string-buffer – jldupont