Help, multidimensional list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Crawley

    Help, multidimensional list

    Im trying to create a list of lists and for some reason its not working:

    class Tile:
    _next = { 'n':None, 'ne':None, 'e':None, 'se':None, 's':None,
    'sw':None, 'w':None, 'nw':None }


    def blankGridCanvas ( maxx, maxy ):
    grid = []
    for y in xrange( 0, maxy ):
    yline = []
    for x in xrange( 0, maxx ):
    t = Tile()
    if y != 0:
    t._next[ 'n' ] = grid[ y - 1 ][ x ]
    t._next[ 'n' ]._next[ 's' ] = t

    yline.append( t )
    grid.append( yline )

    for y in xrange( 0, maxy ):
    for x in xrange( 0, maxx ):
    print grid[ x ][ y ], grid[ x ][ y ]._next
    return grid

    now by my reconing this should be a list of lists with each element being
    an instance of Tile, and some references being manipulated to point to each
    other. But strangly no, its actually a list of lists where each element is
    the SAME instance of Tile, so I change one, i change them ALL!

    Whats going on and how do I solve it?

    Rich
  • Christopher Koppler

    #2
    Re: Help, multidimensiona l list

    On Sun, 28 Dec 2003 14:27:41 +0000, Crawley
    <crawley.storm@ IGetTooMuchSpam AsItIs.com> wrote:
    [color=blue]
    >Im trying to create a list of lists and for some reason its not working:
    >
    >class Tile:
    > _next = { 'n':None, 'ne':None, 'e':None, 'se':None, 's':None,
    >'sw':None, 'w':None, 'nw':None }
    >
    >
    >def blankGridCanvas ( maxx, maxy ):
    > grid = []
    > for y in xrange( 0, maxy ):
    > yline = []
    > for x in xrange( 0, maxx ):
    > t = Tile()
    > if y != 0:
    > t._next[ 'n' ] = grid[ y - 1 ][ x ]
    > t._next[ 'n' ]._next[ 's' ] = t
    >
    > yline.append( t )
    > grid.append( yline )
    >
    > for y in xrange( 0, maxy ):
    > for x in xrange( 0, maxx ):
    > print grid[ x ][ y ], grid[ x ][ y ]._next
    > return grid
    >
    >now by my reconing this should be a list of lists with each element being
    >an instance of Tile,[/color]

    You only create a *class* Tile, and provide no way to create
    *instances* of it - so much like with static variables in other
    languages, you only have one 'instance' here. To be able to create
    separate instances, you need a constructor in your class to
    instantiate every instance, like so:

    class Tile:
    def __init__(self):
    self._next = { 'n':None, 'ne':None, 'e':None, 'se':None,
    's':None, 'sw':None, 'w':None, 'nw':None }


    and some references being manipulated to point to each[color=blue]
    >other. But strangly no, its actually a list of lists where each element is
    >the SAME instance of Tile, so I change one, i change them ALL!
    >
    >Whats going on and how do I solve it?[/color]

    Any gurus around can certainly much better explain what's going on and
    why...

    --
    Christopher

    Comment

    • Terry Reedy

      #3
      Re: Help, multidimensiona l list


      "Christophe r Koppler" <klapotec@chell o.at> wrote in message
      news:jbrtuvg9fh 5e0hdj0d6vseo72 87r94sa7d@4ax.c om...[color=blue]
      > On Sun, 28 Dec 2003 14:27:41 +0000, Crawley
      > <crawley.storm@ IGetTooMuchSpam AsItIs.com> wrote:
      >[color=green]
      > >Im trying to create a list of lists and for some reason its not working:
      > >
      > >class Tile:
      > > _next = { 'n':None, 'ne':None, 'e':None, 'se':None, 's':None,
      > >'sw':None, 'w':None, 'nw':None }
      > >
      > >
      > >def blankGridCanvas ( maxx, maxy ):
      > > grid = []
      > > for y in xrange( 0, maxy ):
      > > yline = []
      > > for x in xrange( 0, maxx ):
      > > t = Tile()
      > > if y != 0:
      > > t._next[ 'n' ] = grid[ y - 1 ][ x ]
      > > t._next[ 'n' ]._next[ 's' ] = t
      > >
      > > yline.append( t )
      > > grid.append( yline )
      > >
      > > for y in xrange( 0, maxy ):
      > > for x in xrange( 0, maxx ):
      > > print grid[ x ][ y ], grid[ x ][ y ]._next
      > > return grid
      > >
      > >now by my reconing this should be a list of lists with each element[/color][/color]
      being[color=blue][color=green]
      > >an instance of Tile,[/color]
      >
      > You only create a *class* Tile, and provide no way to create
      > *instances* of it[/color]

      No, the line 't=Tile()' does create a separate Tile for each grid position.
      However, there is only one class attribute shared by all instances.
      [color=blue]
      > - so much like with static variables in other
      > languages, you only have one 'instance' here. To be able to create
      > separate instances, you need a constructor in your class to
      > instantiate every instance, like so:
      >
      > class Tile:
      > def __init__(self):
      > self._next = { 'n':None, 'ne':None, 'e':None, 'se':None,
      > 's':None, 'sw':None, 'w':None, 'nw':None }[/color]

      You do not need __init__ for separate instances, but do need it to give
      each instance its own map.
      [color=blue]
      > and some references being manipulated to point to each[color=green]
      > >other. But strangly no, its actually a list of lists where each element[/color][/color]
      is[color=blue][color=green]
      > >the SAME instance of Tile, so I change one, i change them ALL![/color][/color]

      As stated above, you do have separate instances but only one map attached
      to the class instead of a separate map for each instance. Koppler's
      __init__ will fix this.

      Terry J. Reedy


      Comment

      • Christopher Koppler

        #4
        Re: Help, multidimensiona l list

        On Sun, 28 Dec 2003 17:50:07 -0500, "Terry Reedy" <tjreedy@udel.e du>
        wrote:
        [color=blue]
        >
        >"Christophe r Koppler" <klapotec@chell o.at> wrote in message
        >news:jbrtuvg9f h5e0hdj0d6vseo7 287r94sa7d@4ax. com...[color=green]
        >> On Sun, 28 Dec 2003 14:27:41 +0000, Crawley
        >> <crawley.storm@ IGetTooMuchSpam AsItIs.com> wrote:
        >>[color=darkred]
        >> >Im trying to create a list of lists and for some reason its not working:
        >> >
        >> >class Tile:
        >> > _next = { 'n':None, 'ne':None, 'e':None, 'se':None, 's':None,
        >> >'sw':None, 'w':None, 'nw':None }
        >> >
        >> >[/color][/color][/color]
        [snip some code][color=blue][color=green][color=darkred]
        >> >
        >> >now by my reconing this should be a list of lists with each element[/color][/color]
        >being[color=green][color=darkred]
        >> >an instance of Tile,[/color]
        >>
        >> You only create a *class* Tile, and provide no way to create
        >> *instances* of it[/color]
        >
        >No, the line 't=Tile()' does create a separate Tile for each grid position.
        >However, there is only one class attribute shared by all instances.[/color]

        Yes, thanks for clearing that up. That is what I probably meant and
        didn't know how to say. Too merry Xmas this year ;-)
        [color=blue]
        >[color=green]
        >> - so much like with static variables in other
        >> languages, you only have one 'instance' here. To be able to create
        >> separate instances, you need a constructor in your class to
        >> instantiate every instance, like so:
        >>
        >> class Tile:
        >> def __init__(self):
        >> self._next = { 'n':None, 'ne':None, 'e':None, 'se':None,
        >> 's':None, 'sw':None, 'w':None, 'nw':None }[/color]
        >
        >You do not need __init__ for separate instances, but do need it to give
        >each instance its own map.[/color]

        I think that's what I meant with the comparison to static variables,
        but should have read static class attributes.
        [color=blue]
        >[color=green]
        >> and some references being manipulated to point to each[color=darkred]
        >> >other. But strangly no, its actually a list of lists where each element[/color][/color]
        >is[color=green][color=darkred]
        >> >the SAME instance of Tile, so I change one, i change them ALL![/color][/color]
        >
        >As stated above, you do have separate instances but only one map attached
        >to the class instead of a separate map for each instance. Koppler's
        >__init__ will fix this.
        >
        >Terry J. Reedy
        >[/color]


        --
        Christopher

        Comment

        Working...