2021年3月9日星期二

AttributeError in Python when attempting to initialize a class in a class

I am attempting to call a class into a class's initializing method, but I am receiving an attribute error. Does anyone have any ideas why this might be occurring? I was able to do this exact thing with another class, but it doesn't seem to like the Movement() class I made.

The add_piece() method in Board() seems to be the one causing the issues.

class Movement:      """      Class to help separate movements from the board methods      """      def __init__(self):          pass        def ind(self, pos):          """          Converts the string input to an easier index for the board          """          row = int(pos[1:]) - 1          column = self.letter_to_column(pos)          return row, column        def letter_to_column(self, pos):          """          Method to convert string letters of columns to int to index          """          column_dict = {}          column_dict['a'] = 0          column_dict['b'] = 1          column_dict['c'] = 2          column_dict['d'] = 3          column_dict['e'] = 4          column_dict['f'] = 5          column_dict['g'] = 6          column_dict['h'] = 7          column_dict['i'] = 8          return column_dict[pos[0]]    # BOARD -------------------------------------------------------------------  class Board:      """      Represents the board and its pieces      """      def __init__(self):          """          Builds the board and places the pieces in its spots          """          self._board = []          for i in range(10):              self._board.append([None for i in range(9)])          self.place_pieces()            self._move = Movement()        def add_piece(self, pos, piece):          """          pos is a string of a letter (column) and a number (row)          for example pos = 'a2' which is the first column in the second row          """          self._board[self._move.ind(pos)[0]][self._move.ind(pos)[1]] = piece  
https://stackoverflow.com/questions/66556960/attributeerror-in-python-when-attempting-to-initialize-a-class-in-a-class March 10, 2021 at 09:06AM

没有评论:

发表评论