On 1/1/21 2:00 PM, Jonathan Fine wrote:
> 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
>
Second problem first, Two numbers that are equal may or might not be
stored in the same object. It is unspecified.

As to the first, just because to points have equal values doesn't mean
that we have recursion.

That is why I put the id()s in the set, the id()s are by definition
hashable, and an object always has the same id() and no two different
objects can have the same id().

Simple example:

list1 = [ ['one', 'two'], ['one', 'two'] ]

The first and second member of the above list are equal, but not the
same object. Thus there is no reason to want to try to trim the second
from an output.


list1 = ['one', 'two']

list2 = [list1, list1]

now the first and second member of the list list2 are both equal and the
same object. Perhaps we might want to indicate this in a dump, but maybe
not.

list1 = ['one', 'two']

list1.append(list11)

Now the third element of the list list1 IS the same object as list1, so
we definitely want to not try and show it when we display the value of
list1, but instead indicate that we have recursion.

Even further:

list1 = ['one', 'two']

list1.append(list1)

list2 = ['one', 'two', list1]

Now list2 will be equal to list1 but is a distinct object. Now the
expansion of list2 will be:

['one', 'two', ['one', 'two', [...]]]

We don't want to test for EQUAL nodes, we want to test for SAME nodes
(same object, same id())

-- 
Richard Damon
_______________________________________________
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/QVRB4IT3WT6NS3L64CNL2Z2QLCH5DO2H/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to