Deleting objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • user@domain.invalid

    Deleting objects

    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

  • Terry Reedy

    #2
    Re: Deleting objects


    <user@domain.in valid> wrote in message
    news:c542f8acd7 55de732c9da8fc3 9fbb50a@news.te ranews.com...[color=blue]
    > 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[/color]

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

    tjr


    Comment

    • Mark McEahern

      #3
      Re: Deleting objects

      On Wed, 2004-01-14 at 17:23, user@domain.inv alid wrote:[color=blue]
      > 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.[/color]
      [color=blue]
      > but when I do:
      >
      > del foo.bar[0]
      >
      > Python says:
      > object doesn't support item deletion[/color]

      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


      Comment

      • Shalabh Chaturvedi

        #4
        Re: Deleting objects

        user@domain.inv alid wrote:[color=blue]
        > 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.[/color]

        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).
        [color=blue]
        >
        > 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?[/color]

        What is the type of foo.bar?
        [color=blue]
        >
        > What if I wanted to remove the array element
        > but still have the object exist?[/color]

        del foo.bar

        This will remove the reference foo.bar.
        [color=blue]
        > What happens if I succeed in destroying an object
        > that other objects still think they are referencing?[/color]

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

        --
        Shalabh



        Comment

        Working...