del

Python del statement

非 Y 不嫁゛ 提交于 2019-11-27 02:38:32
问题 Calling del on a variable in Python. Does this free the allocated memory immediately or still waiting for garbage collector to collect? Like in java, explicitly calling del has no effect on when the memory will be freed. 回答1: The del statement doesn't reclaim memory. It removes a reference, which decrements the reference count on the value. If the count is zero, the memory can be reclaimed. CPython will reclaim the memory immediately, there's no need to wait for the garbage collector to run.

Delete all objects in a list

六月ゝ 毕业季﹏ 提交于 2019-11-27 02:03:21
问题 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 20 gigs of ram if I don't delete it). Here is a little code to illustrate what I trying to do: class test: def __init__(self): self.a = "Hello World" def kill(self): del self a = test() b = test() c = [a,b] print("1)Before:",a,b) for i in c: del i for i in c: i.kill() print("2)After:",a,b) A and B are my objects. C is a

Python attributeError on __del__

一曲冷凌霜 提交于 2019-11-26 17:48:27
问题 I have a python class object and I want to assign the value of one class variable class Groupclass(Workerclass): """worker class""" count = 0 def __init__(self): """initialize time""" Groupclass.count += 1 self.membercount = 0; self.members = [] def __del__(self): """delte a worker data""" Groupclass.count -= 1 if __name__ == "__main__": group1 = Groupclass() This execution result is correct, but there's an error message that says: Exception AttributeError: "'NoneType' object has no attribute

How does python close files that have been gc'ed?

懵懂的女人 提交于 2019-11-26 12:48:27
问题 I had always assumed that a file would leak if it was opened without being closed, but I just verified that if I enter the following lines of code, the file will close: >>> f = open(\'somefile.txt\') >>> del f Just out of sheer curiosity, how does this work? I notice that file doesn\'t include a __ del __ method. 回答1: In CPython, at least, files are closed when the file object is deallocated. See the file_dealloc function in Objects/fileobject.c in the CPython source. Dealloc methods are sort

I don't understand this python __del__ behaviour

白昼怎懂夜的黑 提交于 2019-11-26 11:52:50
Can someone explain why the following code behaves the way it does: import types class Dummy(): def __init__(self, name): self.name = name def __del__(self): print "delete",self.name d1 = Dummy("d1") del d1 d1 = None print "after d1" d2 = Dummy("d2") def func(self): print "func called" d2.func = types.MethodType(func, d2) d2.func() del d2 d2 = None print "after d2" d3 = Dummy("d3") def func(self): print "func called" d3.func = types.MethodType(func, d3) d3.func() d3.func = None del d3 d3 = None print "after d3" The output (note that the destructor for d2 is never called) is this (python 2.7)

Delete an element from a dictionary

岁酱吖の 提交于 2019-11-26 09:12:59
Is there a way to delete an item from a dictionary in Python? Additionally, how can I delete an item from a dictionary to return a copy (i.e., not modifying the original)? Greg Hewgill The del statement removes an element: del d[key] However, this mutates the existing dictionary so the contents of the dictionary changes for anybody else who has a reference to the same instance. To return a new dictionary, make a copy of the dictionary: def removekey(d, key): r = dict(d) del r[key] return r The dict() constructor makes a shallow copy . To make a deep copy, see the copy module . Note that making

I don't understand this python __del__ behaviour

你说的曾经没有我的故事 提交于 2019-11-26 02:38:07
问题 Can someone explain why the following code behaves the way it does: import types class Dummy(): def __init__(self, name): self.name = name def __del__(self): print \"delete\",self.name d1 = Dummy(\"d1\") del d1 d1 = None print \"after d1\" d2 = Dummy(\"d2\") def func(self): print \"func called\" d2.func = types.MethodType(func, d2) d2.func() del d2 d2 = None print \"after d2\" d3 = Dummy(\"d3\") def func(self): print \"func called\" d3.func = types.MethodType(func, d3) d3.func() d3.func =