1

I have a very simple script and want to observe the behavior of the return statement in Spyder. I'm wondering why the return statement value is not included in the Variable explorer.

enter image description here

psrpsrpsr
  • 457
  • 1
  • 4
  • 12
  • 1
    did you try to step into your function until you are on line 11? result is only ever declared on line 10 ... and hidden in the function scope of line 9 – Patrick Artner Jan 01 '21 at 19:19
  • 1
    if you didn't run line 10 then this variable don't exist yet. – furas Jan 01 '21 at 19:30
  • @PatrickArtner when I 'Step' across each line, 'result' does not appear in Variable Explorer. It does indeed add 'result' to VE. However this process steps into iostream.py, threading.py, and other scripts outside of the scope of my debugging process. I guess I have some more research to do on why that is, and best practices for debugging. – psrpsrpsr Jan 02 '21 at 15:28

2 Answers2

0

I think it is because you are simply creating the function sum_vars for every iteration, but you are not actually calling it.

You need to call your function and assign the output of the function to a variable. So for example, inside your for loop you could have:

test = sum_vars(x, y)

And you should be able to say the variable test in your variable explorer. However, its value will only be that of the last iteration of the for loop.

Mahmoud Gabr
  • 109
  • 2
  • 12
0

The print function does not return anything to you. It just prints to the console. You have to assign the sum_vars(x, y) to a variable (e.g. sum = sum_vars(x, y)). Due to the fact that you have some iterations, you may consider adding the sum value to a list for each iteration.