When is del useful in python?

前端 未结 21 1847
野趣味
野趣味 2020-11-22 11:58

I can\'t really think of any reason why python needs the del keyword (and most languages seem to not have a similar keyword). For instance, rather than deletin

相关标签:
21条回答
  • 2020-11-22 12:29

    Here goes my 2 cents contribution:

    I have a optimization problem where I use a Nlopt library for it. I initializing the class and some of its methods, I was using in several other parts of the code.

    I was having ramdom results even if applying the same numerical problem.

    I just realized that by doing it, some spurius data was contained in the object when it should have no issues at all. After using del, I guess the memory is being properly cleared and it might be an internal issue to that class where some variables might not be liking to be reused without proper constructor.

    0 讨论(0)
  • 2020-11-22 12:31

    Just another thinking.

    When debugging http applications in framework like Django, the call stack full of useless and messed up variables previously used, especially when it's a very long list, could be very painful for developers. so, at this point, namespace controlling could be useful.

    0 讨论(0)
  • 2020-11-22 12:32

    There is a specific example of when you should use del (there may be others, but I know about this one off hand) when you are using sys.exc_info() to inspect an exception. This function returns a tuple, the type of exception that was raised, the message, and a traceback.

    The first two values are usually sufficient to diagnose an error and act on it, but the third contains the entire call stack between where the exception was raised and where the the exception is caught. In particular, if you do something like

    try:
        do_evil()
    except:
        exc_type, exc_value, tb = sys.exc_info()
        if something(exc_value):
            raise
    

    the traceback, tb ends up in the locals of the call stack, creating a circular reference that cannot be garbage collected. Thus, it is important to do:

    try:
        do_evil()
    except:
        exc_type, exc_value, tb = sys.exc_info()
        del tb
        if something(exc_value):
            raise
    

    to break the circular reference. In many cases where you would want to call sys.exc_info(), like with metaclass magic, the traceback is useful, so you have to make sure that you clean it up before you can possibly leave the exception handler. If you don't need the traceback, you should delete it immediately, or just do:

    exc_type, exc_value = sys.exc_info()[:2]
    

    To avoid it all together.

    0 讨论(0)
  • 2020-11-22 12:32

    I've found del to be useful for pseudo-manual memory management when handling large data with Numpy. For example:

    for image_name in large_image_set:
        large_image = io.imread(image_name)
        height, width, depth = large_image.shape
        large_mask = np.all(large_image == <some_condition>)
        # Clear memory, make space
        del large_image; gc.collect()
    
        large_processed_image = np.zeros((height, width, depth))
        large_processed_image[large_mask] = (new_value)
        io.imsave("processed_image.png", large_processed_image)
    
        # Clear memory, make space
        del large_mask, large_processed_image; gc.collect()
    

    This can be the difference between bringing a script to a grinding halt as the system swaps like mad when the Python GC can't keep up, and it running perfectly smooth below a loose memory threshold that leaves plenty of headroom to use the machine to browse and code while it's working.

    0 讨论(0)
  • 2020-11-22 12:33

    Deleting a variable is different than setting it to None

    Deleting variable names with del is probably something used rarely, but it is something that could not trivially be achieved without a keyword. If you can create a variable name by writing a=1, it is nice that you can theoretically undo this by deleting a.

    It can make debugging easier in some cases as trying to access a deleted variable will raise an NameError.

    You can delete class instance attributes

    Python lets you write something like:

    class A(object):
        def set_a(self, a):
            self.a=a
    a=A()
    a.set_a(3)
    if hasattr(a, "a"):
        print("Hallo")
    

    If you choose to dynamically add attributes to a class instance, you certainly want to be able to undo it by writing

    del a.a
    
    0 讨论(0)
  • 2020-11-22 12:34

    Yet another niche usage: In pyroot with ROOT5 or ROOT6, "del" may be useful to remove a python object that referred to a no-longer existing C++ object. This allows the dynamic lookup of pyroot to find an identically-named C++ object and bind it to the python name. So you can have a scenario such as:

    import ROOT as R
    input_file = R.TFile('inputs/___my_file_name___.root')
    tree = input_file.Get('r')
    tree.Draw('hy>>hh(10,0,5)')
    R.gPad.Close()
    R.hy # shows that hy is still available. It can even be redrawn at this stage.
    tree.Draw('hy>>hh(3,0,3)') # overwrites the C++ object in ROOT's namespace
    R.hy # shows that R.hy is None, since the C++ object it pointed to is gone
    del R.hy
    R.hy # now finds the new C++ object
    

    Hopefully, this niche will be closed with ROOT7's saner object management.

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