0

I am new to using the python interactive window and I like it but it seems to clear local variables in between runs, so if I run something like


def main():
    dates = '2012152'
    # %%
    print(dates)              # want to just run this

    # %%


if __name__ == '__main__':
    main()

# or even
main()

all at once it works fine but then if I just run the middle cell I get "dates not defined" error. It works outside of the function because apparently a global variable is saved:

dates = '2012152'
# %%
print(dates)             # this works if this cell is run

# %%

Is there any way to get a similar behavior inside a function? If not it doesn't seem useful to me at all (maybe I have designed my code badly?).

2 Answers2

1

Cells are a great way to experiment with flat code, but they are limited when working nested inside functions. One way to work around this is to use a regular built-in python debugger and set a breakpoint within the function.

Here is a process I use for experimenting with code inside a function:

  1. Set a breakpoint in the function, after the code you want to experiment with and start debugging.
  2. Make any necessary changes to the code.
  3. Select the lines that you've changed and that you want to run again. Make sure to include all the indentations as well.
  4. Right-click and select Evaluate in Debug Console.

This will allow you to run the code one line at a time, see results, and make any necessary adjustments as you go along.

The process could be further improved by binding a keyboard shortcut to the this command.

Radzor
  • 162
  • 1
  • 7
0

Yes, print(dates) will not run as the dates variable isn't in scope, unless the function main is called and even then dates will be only in the local scope of the function, not in global scope.

So to print it outside the function, you need to first define it.

SuPythony
  • 875
  • 8
  • 23
  • So is there no way for the interactive terminal to pause the script somewhere inside the function, keeping variables in memory, then a single line run several times with tweaks? I thought that was the point of an interactive mode like this – Thomas Dewitt Dec 16 '21 at 18:37
  • By interactive terminal, do you mean a jypter notebook? – SuPythony Dec 16 '21 at 19:48
  • No, I think it's distinct from jupyter compatibility but might use that under the hood or something. I'm talking about the window as in "run current file in interactive window" which is an option when you right click in the editor. – Thomas Dewitt Dec 17 '21 at 04:16
  • The default python IDLE? – SuPythony Dec 17 '21 at 05:24
  • In VS Code. It's just called interactive window, it came preinstalled, or at least with the standard Python package. https://code.visualstudio.com/docs/python/jupyter-support-py#_python-interactive-window – Thomas Dewitt Dec 18 '21 at 15:46
  • The individual cells of code can't be paused in-between execution. – SuPythony Dec 18 '21 at 16:45
  • But they can if they are not inside a function, as I explained. I want to know why there is a difference in the two cases I gave. – Thomas Dewitt Dec 19 '21 at 14:54
  • The cells have independent memories. So if you declare a variable in one cell and then run the other, it will not have that variable in its memory – SuPythony Dec 20 '21 at 03:46
  • Again, if it's out of a function, a variable declared in a previous cell _is_ retained in memory and will be stored for future cells. Not the case in functions. – Thomas Dewitt Dec 22 '21 at 17:22
  • That's because in the case of functions it's retained in the function's local scope and will not be accessible from outside the function – SuPythony Dec 23 '21 at 03:03