Help, multidimensional list

Crawley crawley.storm at IGetTooMuchSpamAsItIs.com
Sun Dec 28 09:27:41 EST 2003


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




More information about the Python-list mailing list