Why isn't my WeakSet empty after deleting the only other reference?
Question: I decided I should familiarize myself with the [`weakref`
library](https://docs.python.org/3/library/weakref.html), so I started messing
around with the collections, thought I was getting the hang of it until this.

    
    
    >>> import weakref
    
    >>> class Greek():
            def __init__(self, name):
                self.name = name
    
    >>> objs = [Greek('alpha'), Greek('gamma'), Greek('zeta')]
    >>> weak_s = weakref.WeakSet()
    
    >>> for obj in objs: weak_s.add(obj)
    

Here I have a WeakSet `weak_s` that I would expect to contain 3 `weakref`s to
each `Greek()` instance, and it does:

    
    
    >>>  weak_s.data
    {<weakref at 0x02ECA690; to 'Greek' at 0x02E5FF90>,
     <weakref at 0x02ECA720; to 'Greek' at 0x02E5FFB0>,
     <weakref at 0x02ECA750; to 'Greek' at 0x02E5FFD0>}
    

Then I would expect that as soon as I get rid of the `objs` name, there won't
be any more "strong references" to each `Greek()` instance, and they will be
garbage collected. But for some reason this doesn't happen.

    
    
    >>> del objs
    >>> weak_s.data
    {<weakref at 0x02ECA750; to 'Greek' at 0x02E5FFD0>}
    

Why is there still a weakref inside my WeakSet? I can use `del` again and it
persists, seemingly no matter what I do.


Answer: You still have the `obj` variable from here:

    
    
    >>> for obj in objs: weak_s.add(obj)
    

That variable retains its last value from the loop, so it's still referring to
the last `Greek` instance, keeping it alive and in the `WeakSet`.

