If there are no other references to some_other_object
throughout the program, then yes, it too will get deleted.
In your case, there are two references: 1) some_other_object
, and 2) myclass.object
.
Deleting myclass
just deletes the second reference. But the first remains.
Python uses a method of garbage collection called 'reference counting'. To summarise it very concisely, Python keeps track of the number of 'references' to each object in memory. If you run del x
, you're decrementing the number of references to the object that x
referred to by 1 (and the name x
no longer refers to that object, of course). Once the number of references to an object reaches 0, it can be garbage collected (i.e. the memory it occupies can be freed).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…