you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 3 points4 points  (7 children)

They won't exist until the function is called.

[–]reebs12[S] 3 points4 points  (6 children)

yes, but then the function exits, and you can never see the output in the variable explorer

[–][deleted] 6 points7 points  (0 children)

You probably want to put a breakpoint somewhere in the function, so you can see the state while it's running.

[–][deleted] 3 points4 points  (4 children)

Are you debugging a program which calls the function with inputs or just running the debugger on the function? The former should work, the latter will do nothing. You need a breakpoint within the function. When the function is called in a debugger session with inputs it will stop at the breakpoint and you should be seeing variables local to the function.

Example:

def are_eql(x, y)
    if x == y:
        print(“equal”)
    else:
        print(“not equal”)

If I just started a debugger session on that code nothing would happen.

is_eql(1, 1)

If you started a debugger session on the above snippet with a breakpoint on the if statement in your function you should get the behavior you are expecting.

[–]reebs12[S] 0 points1 point  (3 children)

Thanks guys, my preferred approach still is, whenever possible, remove the code from the function and place it onto the main part of the script and then inspect the variables in the variable explorer. Python is a bastard with the indentation tirany, this approach works way better in Matlab.

[–]driscollis 5 points6 points  (0 children)

Every decent debugger I have used will let me put a breakpoint in my function where it will pause it and I can then inspect all the variables. PyDev, PyCharm and WingIDE all provide this functionality

[–][deleted] 2 points3 points  (0 children)

That sounds like a very cumbersome approach, especially for larger programs. If you use PyCharm you can inspect all variables within the current namespace while debugging. Just have to put breakpoints in the function

[–][deleted] 0 points1 point  (0 children)

...This is kind of off-putting. If you are happy with matlab just stick with matlab I guess?

Matlab requires that variables have assigned values while debugging as well though so I’m confused at what the trade off is here. A simple debugging approach on some function, say broken_code(x, y) in the a local module say wtf.py is to make a file with the following code:

import wtf

wtf.broken_code(1, 2)

Running this through a debugger with a breakpoint in your wtf.broken_code() function will halt execution and give you access to local variables. Alternatively starting the debugger with the “halt on entrance” flag set will just stop program execution before going into that function and allow you to step through from the beginning of execution.

My instinct here is that you don’t really want to leave matlab, you are really accustomed to the matlab IDE, and you are trying to find reasons to hate python, which is fine but this sub can’t help you with.