Deleting objects

Discussion in 'Python' started by user@domain.invalid, Jan 14, 2004.

  1. Guest

    Say I have an object (foo), that contains an
    array (bar) of references to other objects.

    Now I want to puff some of the objects from the
    array so that I remove the array element, and
    destroy the oject.

    but when I do:

    del foo.bar[0]

    Python says:
    object doesn't support item deletion

    So do I need to define __del__? And what would I
    put there?

    What if I wanted to remove the array element
    but still have the object exist?

    What happens if I succeed in destroying an object
    that other objects still think they are referencing?

    Thanks,

    Toby
    , Jan 14, 2004
    #1
    1. Advertising

  2. On Wed, 2004-01-14 at 17:23, lid wrote:
    > Say I have an object (foo), that contains an
    > array (bar) of references to other objects.
    >
    > Now I want to puff some of the objects from the
    > array so that I remove the array element, and
    > destroy the oject.


    > but when I do:
    >
    > del foo.bar[0]
    >
    > Python says:
    > object doesn't support item deletion


    It'd be helpful if you supplied the code in question. Then again, we
    wouldn't be able to let our imagination wander with what puff might
    mean. <wink>

    Anyway, is this the sort of thing you're talking about?

    #!/usr/bin/env python

    class Foo:

    def __init__(self):
    self.bar = []

    def __str__(self):
    return '<Foo><bar>%s</bar></Foo>' % (str(self.bar),)

    l = range(10)

    foo = Foo()
    foo.bar.append(l)
    del foo.bar[0]
    print 'foo = %s' % (str(foo),)
    print 'l = %s' % (l,)

    Cheers,

    // m
    Mark McEahern, Jan 15, 2004
    #2
    1. Advertising

  3. Terry Reedy Guest

    <> wrote in message
    news:...
    > Say I have an object (foo), that contains an
    > array (bar) of references to other objects.
    >
    > Now I want to puff some of the objects from the
    > array so that I remove the array element, and
    > destroy the oject.
    >
    > but when I do:
    >
    > del foo.bar[0]
    >
    > Python says:
    > object doesn't support item deletion


    What is the actual type of foo.bar? >>>type(foo.bar) # prints what?

    tjr
    Terry Reedy, Jan 15, 2004
    #3
  4. lid wrote:
    > Say I have an object (foo), that contains an
    > array (bar) of references to other objects.
    >
    > Now I want to puff some of the objects from the
    > array so that I remove the array element, and
    > destroy the oject.


    You don't need to jump through hoops for this. All you need to do is:

    del foo

    If there are no other references to the object that was called foo
    above, then (and only then) it will be destroyed.

    Python keeps a reference count for each object. When the count hits 0
    (no references pointing to object) it is destroyed.

    Because of this, attributes of foo (eg foo.bar) will automatically be
    destroyed when foo is destroyed (assuming they don't have references
    from elsewhere).

    >
    > but when I do:
    >
    > del foo.bar[0]
    >
    > Python says:
    > object doesn't support item deletion
    >
    > So do I need to define __del__? And what would I
    > put there?


    What is the type of foo.bar?

    >
    > What if I wanted to remove the array element
    > but still have the object exist?


    del foo.bar

    This will remove the reference foo.bar.

    > What happens if I succeed in destroying an object
    > that other objects still think they are referencing?


    Try as you might, shooting yourself in the foot is pretty hard in Python
    (see above :)

    --
    Shalabh
    Shalabh Chaturvedi, Jan 15, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jimjim
    Replies:
    0
    Views:
    362
    jimjim
    Feb 27, 2004
  2. dasod

    deleting list objects

    dasod, Jul 4, 2003, in forum: C++
    Replies:
    2
    Views:
    359
    dasod
    Jul 4, 2003
  3. Harry Barker
    Replies:
    2
    Views:
    507
    Alf P. Steinbach
    Apr 19, 2006
  4. 7stud
    Replies:
    11
    Views:
    673
    Dennis Lee Bieber
    Mar 20, 2007
  5. crea
    Replies:
    2
    Views:
    398
    Nobody
    Dec 28, 2012
Loading...

Share This Page