cat /dev/brain

Python: del

While testing by hand, I found myself wanted to delete objects that I created as a test from an array after deleting them on GitHub. To do that, I have to do:

del gists[index]

So my initial instinct was to modify the __del__ method on the objects with delete methods. That would look something like this:

class Gist(GitHubCore):
    # snip

    def __del__(self):

    # snip

And would be used:

del gists[index]

But I remembered having read something[1] that discouraged developers from writing their own __del__ function. So I did some Googling and found the documentation about that function on The problem is how Python keeps track of objects. Let's say that you do the following:

g = login(username, password)
gists = g.list_gists()
gist = gists[-1]
gist.edit('description', {'foo.rst' : {'content': open('foo.rst')}})
# Do some other things
del gists[-1]

What del does in this case is decrement the reference count on that object from 2 to 1. Until the reference count reaches 0, the desired equivalent:

del gists[-1]

will not be true.

In otherwords, in a case like above, the person might check their GitHub account and notice that the gist was not actually deleted. This is unexpected behavior obviously and not acceptable for a module used by anyone other than me.