nested lists - utter newbie

Discussion in 'Python' started by rahoool@gmail.com, Jul 1, 2005.

  1. Guest

    Hi,
    why is this possible -
    >>>b = [1,2,3]
    >>>b[2] = b
    >>>b

    [1,2,[...]]
    >>>b[2]

    [1,2,[...]]
    >>>b[2][2][2][2][2]

    [1,2,[...]]

    but this is not -
    >>>x = [1,2,x]

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    NameError: name 'x' is not defined

    thanks
    r_a_h_o_o_l
     
    , Jul 1, 2005
    #1
    1. Advertising

  2. wrote:

    > Hi,
    > why is this possible -
    >>>>b = [1,2,3]
    >>>>b[2] = b
    >>>>b

    > [1,2,[...]]
    >>>>b[2]

    > [1,2,[...]]
    >>>>b[2][2][2][2][2]

    > [1,2,[...]]
    >
    > but this is not -
    >>>>x = [1,2,x]

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > NameError: name 'x' is not defined


    Because the right hand side ('[1,2,x]') is evaluated *before* the value is
    bound to the name 'x' - and at this point there is obviously no name 'x'
    defined.

    --
    Benjamin Niemann
    Email: pink at odahoda dot de
    WWW: http://www.odahoda.de/
     
    Benjamin Niemann, Jul 1, 2005
    #2
    1. Advertising

  3. On Friday 01 July 2005 05:37 am, Benjamin Niemann wrote:
    > wrote:
    >
    > > Hi,
    > > why is this possible -
    > >>>>b = [1,2,3]
    > >>>>b[2] = b
    > >>>>b

    > > [1,2,[...]]
    > >>>>b[2]

    > > [1,2,[...]]
    > >>>>b[2][2][2][2][2]

    > > [1,2,[...]]
    > >
    > > but this is not -
    > >>>>x = [1,2,x]

    > > Traceback (most recent call last):
    > > File "<stdin>", line 1, in ?
    > > NameError: name 'x' is not defined

    >
    > Because the right hand side ('[1,2,x]') is evaluated *before* the value is
    > bound to the name 'x' - and at this point there is obviously no name 'x'
    > defined.


    OTOH, the name "b" was bound to the list, so it can be put into
    the list, just like any other name. At that point, the list contains
    a series of references, one of which happens to be to itself.

    If you are familiar with Unix/Linux filesystems, you've probably
    already seen behavior like this with either symbolic or hard links --
    it's quite possible to create loops so that a directory is contained
    within itself. Such a structure is not strictly a "tree" any more,
    but a "directed graph". Those are the data structures terms for
    them, and you might try a little googling to learn more about them.

    The *really* smart thing is that Python *writes* the list as:

    [1,2,[...]]

    instead of printing a traceback due to "excessive recursion" which
    is what it used to do. This is because the representation method
    was changed to catch such circular references and stick in the "[...]"
    instead.

    --
    Terry Hancock ( hancock at anansispaceworks.com )
    Anansi Spaceworks http://www.anansispaceworks.com
     
    Terry Hancock, Jul 1, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JustSomeGuy

    Sorting lists of lists...

    JustSomeGuy, Jun 17, 2004, in forum: C++
    Replies:
    0
    Views:
    329
    JustSomeGuy
    Jun 17, 2004
  2. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    413
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  3. ardief
    Replies:
    14
    Views:
    744
    Paddy
    Feb 3, 2007
  4. Replies:
    7
    Views:
    343
    George Sakkis
    Apr 11, 2009
  5. Paul Mason

    Utter madness!

    Paul Mason, Jul 14, 2004, in forum: ASP .Net Security
    Replies:
    5
    Views:
    146
    Paul Mason
    Jul 15, 2004
Loading...

Share This Page