On Wednesday, February 28, 2018 at 10:26:26 PM UTC-6, Steven D'Aprano wrote: > On Wed, 28 Feb 2018 18:46:05 -0800, Rick Johnson 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_? This looks like a highly contrived > > example used to (1) merely win the argument, and (2) Bump fib() up one > > position from it's current position as "the worst introductory example > > of how to write a function in the history of programming tutorials" > > > Of course it is a contrived example: it is close to the > simplest possible example of a reference cycle. Its not > intended as an example of useful code.
I support your appeal to simplicity, but surely you will admit that circular references (CR) of this type are extremely rare. The most common usage of CR is the node tree. > (Useful or not, your interpreter better not crash when > faced with a cycle like this.) Well, if you want your language to have CR as a feature, then yes. Some folks believe that CRs are evil. But i'm not one of those people. I believe CRs are useful in some specific cases. But a programmer must use them wisely. > But of course there are uses for objects to hold a > reference to themselves. If you're writing a general > purpose collection (say, a list or dict) then you better be > able to cope with the case that somebody puts your list in > itself: > > L = [] > L.append(L) > > either directly, as above, or indirectly. A good example. And one that is easy to overlook. > And that's the point really: cycles don't just occur when > an object holds a reference to itself. They occur when one > object holds a reference to another object, which holds a > reference to a third, which holds a reference to a fourth > (and so on...), which eventually holds a reference to the > original again. > > Cycles are useful. For example, you might think of a window > which holds a reference to a control, which in turn needs a > reference to its owning window. Or a network which contains > loops, say a network of streets, or networks of disease > transmission [...] Agreed. I don't buy into the religion that _all_ CRs are evil. Those who make such claims are dealing in absolutes. And as Obi- wan warned Anakin, only a Sith deals in absolutes. ;-) -- https://mail.python.org/mailman/listinfo/python-list