0

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()?

juliomalegria
  • 24,229
  • 14
  • 73
  • 89
Hailiang Zhang
  • 17,604
  • 23
  • 71
  • 117
  • 3
    You should go back and accept the most relevant answers to some of our questions. – GWW Nov 23 '11 at 02:20
  • @HailiangZhang, usually after asking, you accept the best answer clicking on the check symbol. You haven't done this, not even once, for all your 10 questions. – juliomalegria Nov 23 '11 at 03:03

1 Answers1

3

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
juliomalegria
  • 24,229
  • 14
  • 73
  • 89