[Tutor] Deleting an instance of an object

Adam Cripps kabads at gmail.com
Thu Sep 23 17:15:43 CEST 2004


On Thu, 23 Sep 2004 08:17:08 -0400, Kent Johnson
<kent_johnson at skillsoft.com> wrote:
> Adam,
> 
> 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)
>    collect_results.append(result)
> 
> 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:
> http://docs.python.org/tut/node7.html#SECTION007300000000000000000)
> 
> 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]
>    article_list.remove(article)
> 
> Kent
<snip>

Kent, 

Thanks very much - I understand this a lot more now. 
 I'll implement your suggestion. 

Thanks again. 

Adam


More information about the Tutor mailing list