I'm having some slight trouble understanding exactly why this creates an
infinite loop:
L = [1, 2]
L.append(L)
In a highly abstract way, I understand it. But if I were asked to write
down what I think L is after the second line executes, I would write:
[1, 2, [1, 2]]
But the correct answer seems to be:
[1, 2, [1, 2 [1, 2 [ .......
I guess what confuses me is that there doesn't seem to be anything
inherent in L itself that would cause this repeat. If you add L to L,
why don't you just get two Ls? Where does the cycle begin? Is this just
a quirk that comes with having a variable include a reference to the
same object the variable references? 8 1517
On 2/13/06, John Salerno <jo******@nospamgmail.com> wrote: I'm having some slight trouble understanding exactly why this creates an infinite loop:
L = [1, 2] L.append(L)
'L' is a pointer to a list. You are now adding that pointer to the
very list it points to.
What you're looking for is:
L = [1, 2]
L.append(L[:])
--
# p.d.
Peter Decker wrote: 'L' is a pointer to a list. You are now adding that pointer to the very list it points to.
I understand that, but I guess I just don't see how this creates
anything other than a list that refers to [1, 2], and then refers again
to [1, 2], to create [1, 2, [1, 2]].
L.append(L) basically creates this, I think:
[1, 2, L]
Now, assuming that's correct, and since L points to the list [1, 2], why
can't '[1, 2]' be substituted for the 'L' and then the list is closed?
On Mon, 2006-02-13 at 16:03, John Salerno wrote: Peter Decker wrote:
'L' is a pointer to a list. You are now adding that pointer to the very list it points to.
I understand that, but I guess I just don't see how this creates anything other than a list that refers to [1, 2], and then refers again to [1, 2], to create [1, 2, [1, 2]].
L.append(L) basically creates this, I think:
[1, 2, L]
Now, assuming that's correct, and since L points to the list [1, 2], why can't '[1, 2]' be substituted for the 'L' and then the list is closed?
L is [1,2] before the append. After the append, L is [1,2,L], which is
[1,2,[1,2,L]], which is [1,2,[1,2,[1,2,L]]] etc into infinity. L
literally contains itself after the append.
HTH,
Carsten.
John Salerno wrote: L.append(L) basically creates this, I think:
[1, 2, L]
Now, assuming that's correct, and since L points to the list [1, 2]
L points to [1, 2, L]
</F>
John Salerno wrote: L.append(L) basically creates this, I think:
[1, 2, L]
Correct. Now, assuming that's correct, and since L points to the list [1, 2],
wrong. You just said it yourself: L is now the list [1, 2, L]
why can't '[1, 2]' be substituted for the 'L' and then the list is closed?
The append method modifies the list in place. The list which contained 1,2
now has a third element which is a reference to itself.
Carsten Haese wrote: L is [1,2] before the append. After the append, L is [1,2,L], which is [1,2,[1,2,L]], which is [1,2,[1,2,[1,2,L]]] etc into infinity. L literally contains itself after the append.
Ah, this perfectly explains it now! I guess I was dancing around this
fact, but now that you've typed it out, I see that L actually changed in
place!
Thanks guys!
Duncan Booth wrote: The append method modifies the list in place.
That's the key that unlocked the mystery for me. ;)
John Salerno wrote: I'm having some slight trouble understanding exactly why this creates an infinite loop:
L = [1, 2] L.append(L)
I tried this with Python 2.3.5 and it handles this tailbiter in a
very pleasantly surprising way: l = [ 0, 1 ] l.append( l ) l
[0, 1, [...]] l[2]
[0, 1, [...]] l[2][2][2][2][2][2][2][0]
1
The [...] I have never seen before anywhere, is it documented?
--
-- Ewald This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Thomas Mailund |
last post by:
Hi group.
I have a problem with some C extensions I am working with and
hope that some of you can help. Basically, I am wrapping a a tree
structure from C where I have python methods for...
|
by: Yves Forkl |
last post by:
I need to transform a directed cyclic graph into a DTD. My approach is
to have one element Pn for each "production" number n, with the
content model comprising the starting vertex number n and the...
|
by: Brian Sabolik |
last post by:
I'm not sure if I've broken any Object Oriented rules or not, but ...
I have projects in 2 different solutions that need to use each other's
methods. Therefore I may have an "update" method in...
|
by: John Doe |
last post by:
Hi all,
Regarding those cyclic dependencies of classes (like cases in which
class A refers to class B and class B to class A): can they ALL be
resolved by forward defining classes, and by...
|
by: Dennis Lerche |
last post by:
Hi
I have a problem regarding cyclic dependency, yeahh I know bad
design. But right at this moment I can't see how it should be
redesigned to avoid this. The problem is that I just can't get it...
|
by: free2cric |
last post by:
Hi,
how to detect head and tail in cyclic doubly link list ?
Thanks,
Cric
|
by: Matthias Kramm |
last post by:
Hi All,
I'm having a little bit of trouble using the "imp" module to
dynamically import modules. It seems that somehow cyclic references of
modules don't work.
I'm unable to get the following...
|
by: fc2004 |
last post by:
Hi,
Is there any tools that could report where cyclic header dependency
happens? this would be useful when working with a large project where
tens or hundreds of headers files may form complex...
|
by: Joe Peterson |
last post by:
I've been doing a lot of searching on the topic of one of Python's more
disturbing issues (at least to me): the fact that if a __del__ finalizer
is defined and a cyclic (circular) reference is...
|
by: mfareed |
last post by:
Can somebody help me with the CRC (Cyclic Redundancy Code) algorithm implementation given below:
There is a three byte packet header.
<--------1st byte-----><--------2nd...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |