I have the following codes:
def fsub():
print 'OK'
def fmain():
a = fsub()
fmain()
Apparently fsub() won't return 'OK' and assign to a in fmain(). However, this is what I want. Is there anyway we can make it without changing fsub()?
I have the following codes:
def fsub():
print 'OK'
def fmain():
a = fsub()
fmain()
Apparently fsub() won't return 'OK' and assign to a in fmain(). However, this is what I want. Is there anyway we can make it without changing fsub()?
When you do a = fsub(), you're trying to assing a the return of fsub(), in this case None (because fsub() doesn't return anything).
The correct thing to do is to redirect the stdout to a file, then call the fsub() function, and the redirect back the stdout to the original stdout:
import sys
def fmain():
sys.stdout = open('output','a')
fsub()
sys.stdout = sys.__stdout__
print 'Output of fsub():'
print open('output').read(),
# added the coma (,) to avoid a new line
Result:
>>> fmain()
Output of fsub():
OK