del

__del__ not implicitly called on object when its last reference gets deleted

__del__ not implicitly called on object when its last reference gets deleted Question: I have a class that starts a thread in its __init__ member and I would like join that thread when the instance of that class is not needed anymore, so I implemented that clean-up code in __del__. It turns out that the …

Total answers: 2

How to delete property?

How to delete property? Question: class C(): @property def x(self): return 0 delattr(C(), ‘x’) >>> AttributeError: can’t delete attribute I’m aware del C.x works, but this deletes the class‘s property; can a class instance‘s property be deleted? Asked By: OverLordGoldDragon || Source Answers: I’m aware del C.x works, but this deletes the class’s property; can …

Total answers: 4

`del` on a package has some kind of memory

`del` on a package has some kind of memory Question: del seems to have some memory which puzzles me. See the following: In [1]: import math In [2]: math.cos(0) Out[2]: 1.0 In [3]: del math.cos In [4]: math.cos(0) ————————————————————————— AttributeError Traceback (most recent call last) <ipython-input-4-9cdcc157d079> in <module>() —-> 1 math.cos(0) AttributeError: module ‘math’ has …

Total answers: 3

Why does del (x) with parentheses around the variable name work in Python?

Why does del (x) with parentheses around the variable name work in Python? Question: Why does this piece of code work the way it does? x = 3 print(dir()) #output indicates that x is defined in the global scope del (x) print(dir()) #output indicates that x is not defined in the global scope My understanding …

Total answers: 2

Delete all objects in a list

Delete all objects in a list Question: 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 …

Total answers: 4

When is del useful in Python?

When is del useful in Python? Question: 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 deleting a variable, one could just assign None to it. And when deleting from a dictionary, a del method could be …

Total answers: 24

I don't understand this python __del__ behaviour

I don't understand this python __del__ behaviour Question: 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 = …

Total answers: 8

Delete an element from a dictionary

Delete an element from a dictionary Question: How do I delete an item from a dictionary in Python? Without modifying the original dictionary, how do I obtain another dict with the item removed? See also How can I remove a key from a Python dictionary? for the specific issue of removing an item (by key) …

Total answers: 19

In Python, is use of `del` statement a code smell?

In Python, is use of `del` statement a code smell? Question: I tend to use it whenever I am working on a prototype script, and: Use a somewhat common variable (such as fileCount), and Have a large method (20+ lines), and Do not use classes or namespaces yet. In this situation, in order to avoid …

Total answers: 2

Which is better in python, del or delattr?

Which is better in python, del or delattr? Question: 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 …

Total answers: 8