REMOVE ME

John Pote johnhpote at o2.co.uk
Mon Apr 11 18:44:25 EDT 2016


On 10/04/2016 04:52, Chris Angelico wrote:
> On Sun, Apr 10, 2016 at 1:46 PM, fan nie <niefan68 at gmail.com> wrote:
>> --
>> https://mail.python.org/mailman/listinfo/python-list
> Sure. I presume you mean something like this:
>
> class Thing:
>      things = []
>      def __init__(self):
>          self.things.append(self)
>      def __del__(self):
>          # Remove me when I'm dead
>          self.things.remove(self)
>
> This ingenious technique guarantees that you never have dead objects
> in your list, by having each object remove itself when it dies.
>
> ChrisA
I'm not quite sure how tongue in cheek ChrisA's reply and the Thing 
class was but it did make me think and wonder if my understanding of 
Python lore was quite right. To my mind it looks like a Zombie or even 
Revenant class.

If all external references to an instance of Thing are deleted there is 
still the reference from the class list 'things []'. In which case will 
not this prevent the instance from being garbage collected and __del__() 
never called on the dead instance and its memory never released. But a 
memory dump could reveal the ghost of the instance. On the other hand a 
code wizard with the right lore could resurect the instance by getting 
the reference to it and bring it back to life -
     revenant = Thing.things[x]

But then I notice 'things' is used as an instance attribute rather than 
a class attribute. All seems to be shrouded in a web of mystery

Regards,
John



More information about the Python-list mailing list