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?

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

6 Comments

Seems like a good idea... Thanks. EDIT: Still have the func() running inside another function. Guess my code is just too messed up?
Could you explain a bit more what do you mean by " the func() running inside another function"?
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.
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.
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.
|
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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.