0

If I try to run the following code:

def func():
    a = 5
    print 'done'
    return a
temp = raw_input('')
if temp == '':
    func()

print func()

Say temp is '' and the function is run. It prints done and returns variable a. How can I print the returned variable without running the function once more, so done isn't printed again?

notTypecast
  • 460
  • 5
  • 16

2 Answers2

3

You should assign the returned value to a variable (e.g. a).

Update: you could either print inside the function (version1) or use global variable (version2)

def func():
    a = 5
    print 'done'
    return a

# version 1: if print doesn't have to be outside of the function
def main():
    temp = raw_input('')
    if temp == '':
        local_a = func()
    else:
        # use else to avoid UnboundLocalError: local variable 'a' referenced
        # before assignment
        local_a = None
    print local_a


if __name__ == "__main__":
    main()


# # version 2: if print have to be outside of the function, then I can only
# # think of using global variable, but it's bad.
# global_a = None
# def main():
#     temp = raw_input('')
#     if temp == '':
#         global global_a
#         global_a = func()

# if __name__ == "__main__":
#     main()
#     print global_a
zyxue
  • 7,904
  • 5
  • 48
  • 74
  • Seems like a good idea... Thanks. EDIT: Still have the func() running inside another function. Guess my code is just too messed up? – notTypecast Aug 29 '15 at 00:48
  • 1
    Could you explain a bit more what do you mean by " the func() running inside another function"? – zyxue Aug 29 '15 at 01:00
  • The raw_input command and the if statement are run inside of another function, which means that variable a becomes a local variable. Also, the code is more complicated, because I use Tkinter, so I can't return anything from that function, making the variable a we assigned as a tag to the result of func() useless. – notTypecast Aug 29 '15 at 09:21
  • See the updated answer if it fits your needs. I agree with @ZeroFunter that using global variable is bad, but I've also encountered cases where I have to when using some third-party libraries. – zyxue Aug 29 '15 at 13:59
  • Well, I see what you mean. I accepted your answer. However, I need to use the variables I create in the function all over the program in lots of functions and even edit them, so I guess I can't avoid using globals. – notTypecast Aug 30 '15 at 15:16
  • Maybe consider passing such a variable to every function you defined and that needs it. – zyxue Aug 31 '15 at 05:12
1

You could use @zyxue's answer above and store the return value to a variable or you could also just not return anything from the function and just assign you final value in a function to a global variable if you have need for that. I should warn you that it isn't good practice to overuse global variables unnecessarily or overly. See: https://stackoverflow.com/a/19158418/4671205

Community
  • 1
  • 1
kenjoe41
  • 280
  • 2
  • 8
  • I did indeed use the global command in this case, but I believe it's okay, as what I was trying to do in the end was just to make the variable created into a function global. I hardly ever use global variables, I avoid them and try to find other work arounds, but sometimes, they just are really helpful. – notTypecast Aug 29 '15 at 09:17