script uses up all memory

Marko Rauhamaa marko at
Fri Mar 7 00:53:03 CET 2014

Chris Angelico <rosuav at>:

> Can you give a useful example of a closure that does create a refloop?

Just the other day, I mentioned the state pattern:

   class MyStateMachine:
       def __init__(self):
           sm = self

           class IDLE:
               def ding(self):
                   sm.state = AT_DOOR()

           class AT_DOOR:

           self.state = IDLE()

       def ding(self):

So we have:

    MyStateMachine instance
      -> MyStateMachine instance.ding
         -> IDLE instance
            -> IDLE instance.ding
               -> MyStateMachine instance

plus numerous others in this example alone.

In general, event-driven programming produces circular references left
and right, and that might come into wider use with asyncio.

I suspect generators might create circular references as well.

Any tree data structure with parent references creates cycles.

In fact, I would imagine most OO designs create a pretty tight mesh of
back-and-forth references.


More information about the Python-list mailing list