[Tutor] Deleting an instance of an object

Kent Johnson kent_johnson at skillsoft.com
Thu Sep 23 14:17:08 CEST 2004


First, I'll try to shed a little light on some misconceptions you seem to have.

The name that you have stored in the collect_results list is not really 
associated with the Article any more, it is just a reference to a string. 
That is why you get the error 'str' object has no attribute '__del__'.

You shouldn't be calling __del__ yourself. __del__ is an internal method 
that implements the del statement.

Objects are deleted from a container by calling del on the container. In 
other words, you don't ask an object to delete itself from a container, you 
ask the container to delete the object.

OK, so how should you accomplish what you want? There are two ways to 
delete from a list, by index and by reference (see 
http://docs.python.org/lib/typesseq-mutable.html). For example, here is a 
simple Article class that has a name and shows the name when printed:

 >>> class Article:
...   def __init__(self, name):
...     self.name = name
...   def __repr__(self):
...     return self.name

Make a few articles and put them in a list:

 >>> a=Article('Learning Python')
 >>> a
Learning Python
 >>> b=Article('Python Networking')
 >>> c=Article('Using Twisted')
 >>> articles = [a, b, c]
 >>> articles
[Learning Python, Python Networking, Using Twisted]

Delete the first article by index:

 >>> del articles[0]
 >>> articles
[Python Networking, Using Twisted]

Delete article b by reference:

 >>> articles.remove(b)
 >>> articles
[Using Twisted]

Notice that in each case I do something with the list, and in each case I 
need to know something about the article object besides its name - either 
its index in the list, or a reference to the actual article object.

So, what does this mean for you? I suggest that you save more information 
about an article in the collect_results list. You could save the 
articlesheld list, and the article object. You don't need the name as it is 
in the article. A simple way to do this is to store a tuple in the list :
   result = (issue_object.articlesheld, article_object)

To print the list, you need to unpack the tuple and get the article name back:
   for article_list, article in collect_results:
     print article.name

(You can read a little more about tuples here: 

To delete an entry, get the list and article from the entry and remove the 
article from the list:
   article_list, article = collect_results[del_choice_int]


At 10:19 AM 9/23/2004 +0100, Adam Cripps wrote:
>I've designed an OOP program, where I keep a catalogue of
>publications. I want to be able to delete an instance of Article
>(Article is a subclass of Issue, which is a subclass of Title, which
>is a subclass of MagazineCollection). Each instance of Article has an
>attribute name (article.name).
>I've created a loop which searches through the article.name(s) and
>found the one I want. The user has specified which one they want to
>delete and I've collected that.
>The problem I'm having is finding out the instantiation of self.name -
>is there a hard and fast way of identifying a particular instance of
>I'm getting the error:
>AttributeError: 'str' object has no attribute '__del__'
>which is strange, as the Deleted is coming from a __del__ function
>that I placed in the Article class.
>proj: http://jiletters.sf.net
>site: http://www.monkeez.org
>wiki: http://wiki.monkeez.org
>Tutor maillist  -  Tutor at python.org

More information about the Tutor mailing list