title: Python: storing object in 2d array and calling its method
body: <p>I'm trying my hand at making a chess app. Code below:</p>
  
  <pre><code>#file containing pieces classes
  class Piece(object):`
  
       name = "piece"
       value = 0
       grid_name = "____"
  
  
  class Pawn(Piece):
  
  # Rules for pawns.
  #If first move, then can move forward two spaces
  
  name = "Pawn"
  value = 1
  grid_name = "_PN_"
  first_move = True
  
  
  #Main file
  from Piece import *
  
  
  
  class GameBoard:
  
     pieces = []
     grid = [][]
  
     def __init__(self):
  
        self.grid[1][0] = self.pieces.append(Pawn())
  
  
  
  currentBoard = GameBoard()
  </code></pre>
  
  <p>I want to call the value variable for the object located at grid[1][0]</p>
  
  <p>It would look something like:</p>
  
  <pre><code> print currentBoard.grid[1][0].value
  </code></pre>
  
  <p>This code doesn't work which tells me I'm missing something regarding the scope of the objects and variables. Is this something that is possible in Python?</p>
  
  <h1>EDIT - Solution</h1>
  
  <p>I did find a solution to this in using the grid list to hold a reference to the indexing of the objects in the pieces list. Code below:</p>
  
  <pre><code>class GameBoard:
  
      # initialize empty board
      grid = [["____" for i in range(8)] for j in range(8)]
      pieces = []
  
      def __init__(self):
  
          self.grid[0][0] = 0
          self.grid[0][1] = 1
          self.grid[0][2] = 2
          self.grid[0][3] = 3
          self.grid[0][4] = 4
          self.grid[0][5] = 5
          self.grid[0][6] = 6
          self.grid[0][7] = 7
          self.grid[1][0] = 8
          self.grid[1][1] = 9
          self.grid[1][2] = 10
          self.grid[1][3] = 11
          self.grid[1][4] = 12
          self.grid[1][5] = 13
          self.grid[1][6] = 14
          self.grid[1][7] = 15
  
  
  pieces = []
  
  pieces.append(Pawn())
  
  #grid will return the integer which can be passed to the other list to pull an 
  #object for using the .value attribute
  
  print pieces[currentBoard.grid[1][0]].value
  </code></pre>