views:

58

answers:

1

Let's say I have created two objects from class foo and now want to combine the two. How, if at all possible, can I accomplish that within a function like this:

def combine(first, second):
    first.value += second.value
    del second #this doesn't work, though first.value *does* get changed

instead of doing something like

def combine(first, second):
    first.value += second.value

in the function and putting del second immediately after the function call?

+3  A: 

No. All del does against names is unbind them. This only removes the local reference. The object will be destroyed when there are no references to it anywhere, or all the references are in a reference loop.

Ignacio Vazquez-Abrams
Objects will be destroyed between the last time you can access them and the end of the program. In CPython, most objects are immediately destroyed and cycles are periodically destroyed (except cycles of objects with `__del__` defined). Of course, there are few good reasons to use reference cycles and fewer to define `__del__`.
Mike Graham