Hi Richard

You wrote

I believe that one solution to detecting the cycles is to create a set
> of the object IDs you have visited and started to print. If you come
> across an ID you have already seen, this point is in a cycle. Sets are
> fairly compact and intentionally fast to search for an item.
>

Indeed. But I see a flaw. The problem is that we want to know about
EQUALITY of nodes, not equality of the ID (memory address in disguise) at
which the node is stored.

In other words, as stated earlier, things are easier and quicker if the
nodes are hashable. Only hashable objects can be stored in a set, and
equality of NODE doesn't imply equality of ID. (The converse is trivially
true.)

Here's an example
    >>> def f(): return 10**1000
    >>> list(map(id, (f(), f())))
    [139927013695536, 139927013696008]

By the way, this surprised me. Would anyone like to explain this?
    >>> id(f()), id(f())
    (139927013695536, 139927013695536)

-- 
Jonathan
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/LTHN7GZYSEXDEMQLXL7UZNHY6Y7XDY5K/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to