如果我有一个包含大量打印语句的函数:
例如:
我想要做的是类似于这样的事情。
例如:
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被调用时,它不会打印任何内容,而是将结果输出到一个对象中。我之后会打印这个对象来获取结果。有没有一种方法可以实现这个功能?同时我也不想修改原始函数。
希望我的解释清晰明了。