How to clear variables in ipython?

前端 未结 7 2292
逝去的感伤
逝去的感伤 2020-12-02 04:56

Sometimes I rerun a script within the same ipython session and I get bad surprises when variables haven\'t been cleared. How do I clear all variables? And is it possible to

相关标签:
7条回答
  • 2020-12-02 05:12

    An quit option in the Console Panel will also clear all variables in variable explorer

    *** Note that you will be loosing all the code which you have run in Console Panel

    0 讨论(0)
  • 2020-12-02 05:13

    %reset seems to clear defined variables.

    0 讨论(0)
  • 2020-12-02 05:23

    In iPython you can remove a single variable like this:

    del x
    
    0 讨论(0)
  • 2020-12-02 05:25

    Apart from the methods mentioned earlier. You can also use the command del to remove multiple variables

    del variable1,variable2
    
    0 讨论(0)
  • 2020-12-02 05:30

    EDITED after @ErdemKAYA comment.

    To erase a variable, use the magic command:

    %reset_selective <regular_expression>
    

    The variables that are erased from the namespace are the one matching the given <regular_expression>.

    Therefore

    %reset_selective -f a 
    

    will erase all the variables containing an a.

    Instead, to erase only a and not aa:

    In: a, aa = 1, 2
    In: %reset_selective -f "^a$"
    In: a  # raise NameError
    In: aa  # returns 2
    

    see as well %reset_selective? for more examples and https://regexone.com/ for a regex tutorial.

    To erase all the variables in the namespace see:

    %reset?
    
    0 讨论(0)
  • 2020-12-02 05:34

    I tried

    %reset -f
    

    and cleared all the variables and contents without prompt. -f does the force action on the given command without prompting for yes/no.

    Wish this helps.. :)

    0 讨论(0)
提交回复
热议问题