Python: delete all variables except one for loops without contaminations

后端 未结 1 1417
执念已碎
执念已碎 2021-01-27 23:05
%reset
%reset -f

and

%reset_selective a
%reset_selective -f a

are usefull Python alternative to the Matlab command \

相关标签:
1条回答
  • 2021-01-27 23:48

    The normal approach to limiting the scope of variables is to use them in a function. When the function is done, its locals disappear.

    In [71]: def foo():
        ...:     a=1
        ...:     b=2
        ...:     c=[1,2,3]
        ...:     d=np.arange(12)
        ...:     print(locals())
        ...:     del(a,b,c)
        ...:     print(locals())
        ...:     
    In [72]: foo()
    {'d': array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11]), 'c': [1, 2, 3], 'a': 1, 'b': 2}
    {'d': array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])}
    

    ==================

    %who_ls returns a list, and can be used on the RHS, as in

    xx = %who_ls
    

    and then that list can be iterated. But note that this is a list of variable names, not the variables themselves.

    for x in xx: 
        if len(x)==1:
            print(x)
            # del(x)  does not work
    

    shows all names of length 1.

    ======================

    A simple way to use %reset_selective is to give the temporary variables a distinctive name, such as a prefix that regex can easily find. For example

    In [198]: temp_a, temp_b, temp_c, x, y = 1,'one string',np.arange(10), 10, [1,23]
    In [199]: who_ls
    Out[199]: ['np', 'temp_a', 'temp_b', 'temp_c', 'x', 'y']
    In [200]: %reset_selective -f temp 
    In [201]: who_ls
    Out[201]: ['np', 'x', 'y']
    

    ====================

    Here's an example of doing this deletion from a list of names. Keep in mind that there is a difference between the actual variable that we are trying to delete, and its name.

    Make some variables, and list of names to delete

    In [221]: temp_a, temp_b, temp_c, x, y = 1,'one string',np.arange(10), 10, [1,23]
    In [222]: dellist=['temp_a', 'temp_c','x']
    

    Get the shell, and the user_ns. who_ls uses the keys from self.shell.user_ns.

    In [223]: ip=get_ipython()
    In [224]: user_ns=ip.user_ns
    
    In [225]: %who_ls
    Out[225]: ['dellist', 'i', 'ip', 'np', 'temp_a', 'temp_b', 'temp_c', 'user_ns', 'x', 'y']
    
    In [226]: for i in dellist:
         ...:     del(user_ns[i])
         ...:     
    In [227]: %who_ls
    Out[227]: ['dellist', 'i', 'ip', 'np', 'temp_b', 'user_ns', 'y']
    

    So we have to look up the names in the user_ns dictionary in order to delete them. Note that this deletion code creates some variables, dellist, i, ip, user_ns.

    ==============

    How many variables are you worried about? How big are they? Scalars, lists, numpy arrays. A dozen or so scalars that can be named with letters don't take up much memory. And if there's any pattern in the generation of the variables, it may make more sense to collect them in a list or dictionary, rather than trying to give each a unique name.

    In general it is better to use functions to limit the scope of variables, rather than use del() or %reset. Occasionally if dealing with very large arrays, the kind that take a meg of memory and can create memory errors, I may use del or just a=None to remove them. But ordinary variables don't need special attention (not even in an ipython session that hangs around for several days).

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