RFC: Proposal: Deterministic Object Destruction

Rick Johnson rantingrickjohnson at gmail.com
Thu Mar 1 17:49:30 EST 2018


On Wednesday, February 28, 2018 at 9:00:37 PM UTC-6, Chris Angelico wrote:
> On Thu, Mar 1, 2018 at 1:46 PM, Rick Johnson
> <rantingrickjohnson at gmail.com> wrote:
> > On Wednesday, February 28, 2018 at 5:02:17 PM UTC-6, Chris Angelico wrote:
> >
> >> Here's one example: reference cycles. When do they get detected?
> >> Taking a really simple situation:
> >>
> >> class Foo:
> >>     def __init__(self):
> >>         self.self = self
> >
> > *shudders*
> >
> > Can you provide a real world example in which you need an
> > object which circularly references _itself_? [...]
> 
> Not off hand, but I can provide an EXTREMELY real-world
> example of a fairly tight loop: exceptions. An exception
> has a reference to the local variables it came from, and
> those locals may well include the exception itself:
> 
> try:
>     1/0
> except Exception as e:
>     print(e)
> 
> The ZeroDivisionError has a reference to the locals, and
> 'e' in the locals refers to that very exception object.

AFAIK, Python allows access to the "locals namespace" in
every scope, so i'm not sure how this specific case is any
more unique than any of the innumerable other cases that
could be presented. (but if i'm missing something, feel free
explain)

Also, i'm not convinced that a reference which is
encapsulated by the current local namespace (even if the
containing dict is an attribute of some object!) is the
equivalent of `self.bar = bar`. (NOTE: i used `bar` instead
of `self` so the comparison would be more clear)

    ## Python 2.x ##
    >>> class Foo(object):
    ...     def __init__(self):
    ...         self.bar = "bar"
    ...         print locals()
    ...
    >>> foo = Foo()
    {'self': <__main__.Foo object at 0x027A6890>}
    >>> foo.__dict__
    {'bar': 'bar'}
    
    
So, yeah. The challenge remains: "Produce a real world
example in which an object stores a reference of itself, and
then provide an acceptable justification for such a thing."



More information about the Python-list mailing list