osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RFC: Proposal: Deterministic Object Destruction


02.03.18 02:49, Rick Johnson ????:
> This is true, but it does not answer the challenge directly
> because function CRs are a consequence of Python _internals_
> *NOT* consequences of a custom class written by a programmer
> which references itself _explicitly_.

This doesn't matter. You question was "Can you provide a real world 
example in which you need an object which circularly references 
_itself_?" Global functions are such objects and unlikely you can write 
any reasonable program without using global functions. And if you have 
reference cycles in such fundamental objects you can't avoid detecting 
and breaking reference cycles in GC.

If you want to see an example that creates a reference cycle in Python 
code, here is yet one real-word example:

     class C:
         def __init__(self, ...):
             ...
             self.foo = self.bar if cond else self.baz
             ...

         def bar(self, ...):
             ...

         def baz(self, ...):
             ...


Depending on condition you need to call either method bar or method baz. 
You save a reference to method as instance attribute, but a method has a 
reference to the instance. This creates a cycle.