-5

I'm trying to make a class which creates empty board objects as part of a board game project. Here's what I came up with :

class Board:
    board = []
    # Initially the board is empty

    def __init__(self, size=9):
        # If n is the size of the board
        # Each line is a list of n colums
        # Each column is initially set as a list of n empty squares
        for line in range(size):
            list = []
            for columns in range(size):
                list.append("O")
            self.board.append(list)
        return None

    def __repr__(self):
        # Lines separator is '\n'
        # Columns separator is ' '
        repr = ''
        for n_line in range(len(self.board)):
            for n_column in range(len(self.board[n_line])):
                repr += self.board[n_line][n_column] + ' '
            repr += '\n'
        return repr
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 6
    If this is **working code** that you think could be improved, see [codereview.se]. If not, please clarify the problem with it. – jonrsharpe Aug 20 '18 at 15:57
  • 2
    `board` is currently an attribute of the `Board` class. That means that each `Board` instance will share a single `board`. – Patrick Haugh Aug 20 '18 at 15:58
  • `self.board = [['O'] * size for _ in range(size)]` – blhsing Aug 20 '18 at 16:03
  • Please check ["Which site?"](https://meta.stackexchange.com/questions/129598/which-computer-science-programming-stack-exchange-do-i-post-in) for general issues and ["Code Review or not?"](https://codereview.meta.stackexchange.com/questions/5777/a-guide-to-code-review-for-stack-overflow-users/5778#5778) – Prune Aug 20 '18 at 16:07
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [On topic](http://stackoverflow.com/help/on-topic), [how to ask](http://stackoverflow.com/help/how-to-ask), and [... the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) apply here. StackOverflow is not a design, coding, research, or tutorial service. – Prune Aug 20 '18 at 16:08
  • 1
    @jonrsharpe Regardless of whether it works or not, this question needs a lot of clarification before it's a decent question anywhere. – Mast Aug 20 '18 at 16:16
  • The “most efficient version" of this code is almost certainly a C extension module with handcoded inline assembly that uses the Python API as little as possible and has as much of the data pre-built in static variables as possible. Are you really interested in that? If not, why are you asking for “more efficient”? – abarnert Aug 20 '18 at 16:22

1 Answers1

1

Well, your class can be improved. As pointed out in the comments, board should be an instance attribute, not a a class attribute which can be concisely initiated with a list comprehension. Moreover, proper use of str methods will drastically shorten your __repr__ as well:

class Board:
    # don't make board a class attribute

    def __init__(self, size=9):
        self.board = [["O"] * size for _ in range(size)]

    def __repr__(self):
        return '\n'.join(' '.join(line) for line in self.board)
        # Or more fanciful
        # return '\n'.join(map(' '.join, self.board))

>>> b = Board()
>>> b
O O O O O O O O O
O O O O O O O O O
O O O O O O O O O
O O O O O O O O O
O O O O O O O O O
O O O O O O O O O
O O O O O O O O O
O O O O O O O O O
O O O O O O O O O
user2390182
  • 72,016
  • 6
  • 67
  • 89