"""
Example from https://pymotw.com/2/gc/index.html
translated to Python3
"""

import gc
import pprint

class Graph:
    def __init__(self, name):
        self.name = name
        self.next = None
    def set_next(self, next):
        print('Linking nodes {0}.next = {1}'.format(str(self), str(next)))
        self.next = next
    def __repr__(self):
        return '{0}({1})'.format(str(self.__class__.__name__), self.name)
    #def __str__(self):
    #    return '{0}({1})'.format(str(self.__class__.__name__), self.name)

# Construct a graph cycle
one = Graph('one')
two = Graph('two')
three = Graph('three')
one.set_next(two)
two.set_next(three)
three.set_next(one)
print('')
print('three refers to:')
for r in gc.get_referents(three):
    pp = pprint.PrettyPrinter()
    pp.pprint(r)
