del

Which is better in python, del or delattr?

若如初见. 提交于 2019-11-28 15:21:39
This may be silly, but it's been nagging the back of my brain for a while. Python gives us two built-in ways to delete attributes from objects, the del command word and the delattr built-in function. I prefer delattr because it I think its a bit more explicit: del foo.bar delattr(foo, "bar") But I'm wondering if there might be under-the-hood differences between them. The first is more efficient than the second. del foo.bar compiles to two bytecode instructions: 2 0 LOAD_FAST 0 (foo) 3 DELETE_ATTR 0 (bar) whereas delattr(foo, "bar") takes five: 2 0 LOAD_GLOBAL 0 (delattr) 3 LOAD_FAST 0 (foo) 6

Delete field from standard Django model

断了今生、忘了曾经 提交于 2019-11-28 12:48:17
NOTE : this was asked before AbstractUser existed, which is probably what you'd want to use these days. Basically I would like to delete the default email field from the default Django User class... class MyUser(User): field = models.CharField(max_length = 10) a = 'hello' def b(self): print 'world' del User.email del MyUser.email del Myuser.field All these give AttributeError. Deleting methods or attributes in the following way though works fine: del MyUser.a del MyUser.b So I'm curious why this doesn't work; what type of things are these model fields? Another thing I tried was overwriting

Python del statement

爷,独闯天下 提交于 2019-11-28 09:45:12
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. 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. In fact, the garbage collector is only needed for reclaiming cyclic structures. As Waleed Khan says in his

Delete all objects in a list

牧云@^-^@ 提交于 2019-11-28 08:04:25
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 list of these two objects. I'm trying to delete it definitely with a for-loop in C: one time with DEL and

How to delete every reference of an object in Python?

爱⌒轻易说出口 提交于 2019-11-28 00:44:01
问题 Supose you have something like: x = "something" b = x l = [b] How can you delete the object only having one reference, say x? del x won't do the trick; the object is still reachable from b, for example. 回答1: No no no. Python has a garbage collector that has very strong territory issues - it won't mess with you creating objects, you don't mess with it deleting objects. Simply put, it can't be done, and for a good reason. If, for instance, your need comes from cases of, say, caching algorithms

Which is better in python, del or delattr?

混江龙づ霸主 提交于 2019-11-27 09:09:51
问题 This may be silly, but it's been nagging the back of my brain for a while. Python gives us two built-in ways to delete attributes from objects, the del command word and the delattr built-in function. I prefer delattr because it I think its a bit more explicit: del foo.bar delattr(foo, "bar") But I'm wondering if there might be under-the-hood differences between them. 回答1: The first is more efficient than the second. del foo.bar compiles to two bytecode instructions: 2 0 LOAD_FAST 0 (foo) 3

Python attributeError on __del__

我们两清 提交于 2019-11-27 09:05:58
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 'count'" in <bound method Groupclass.__del__ of <__main__.Groupclass instance at 0x00BA6710>> ignored

__del__ method being called in python when it is not expected

大城市里の小女人 提交于 2019-11-27 07:57:14
问题 I am new to python and have been working through the examples in Swaroop CH's "A Byte of Python". I am seeing some behavior with the __del__ method that is puzzling me. Basically, if I run the following script (in Python 2.6.2) class Person4: '''Represents a person''' population = 0 def __init__(self, name): '''Initialize the person's data''' self.name = name print 'Initializing %s'% self.name #When the person is created they increase the population Person4.population += 1 def __del__(self):

Delete field from standard Django model

空扰寡人 提交于 2019-11-27 07:21:35
问题 NOTE : this was asked before AbstractUser existed, which is probably what you'd want to use these days. Basically I would like to delete the default email field from the default Django User class... class MyUser(User): field = models.CharField(max_length = 10) a = 'hello' def b(self): print 'world' del User.email del MyUser.email del Myuser.field All these give AttributeError. Deleting methods or attributes in the following way though works fine: del MyUser.a del MyUser.b So I'm curious why

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

China☆狼群 提交于 2019-11-27 05:34:17
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. 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-of like __del__ for C types, except without some of the problems inherent to __del__ . S.Lott Hence the with