Delete all objects in a list

后端 未结 4 509
小蘑菇
小蘑菇 2020-12-09 07:46

I create many object then I store in a list. But I want to delete them after some time because I create news one and don\'t want my memory goes high (in my case, it jumps to

4条回答
  •  难免孤独
    2020-12-09 08:09

    If the goal is to delete the objects a and b themselves (which appears to be the case), forming the list [a, b] is not helpful. Instead, one should keep a list of strings used as the names of those objects. These allow one to delete the objects in a loop, by accessing the globals() dictionary.

    c = ['a', 'b']
    # create and work with a and b    
    for i in c:
        del globals()[i]
    

提交回复
热议问题