Enter Interactive Mode In Python

后端 未结 7 810
日久生厌
日久生厌 2021-01-30 06:42

I\'m running my Python program and have a point where it would be useful to jump in and see what\'s going on, and then step out again. Sort of like a temporary console mode.

7条回答
  •  日久生厌
    2021-01-30 07:15

    code.interact() seems to work somehow:

    >>> import code
    >>> def foo():
    ...     a = 10
    ...     code.interact(local=locals())
    ...     return a
    ... 
    >>> foo()
    Python 3.6.5 (default, Apr  1 2018, 05:46:30) 
    [GCC 7.3.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    (InteractiveConsole)
    >>> a
    10
    

    Ctrl+Z returns to the "main" interpreter.

    You can read the locals, but modifying them doesn't seem to work this way.

提交回复
热议问题