I am trying to be more Pythonic and less C++. However the limit of all lines to a maximum of 79 characters creates everyday struggle. Here is one example:
def list_of_possible_moves(self): # version 1
return [] if self._state != Game.play else [index for index, square in enumerate(self._board) if square == TicTacToe.square_free]
It is 138 characters long and has to be broken. (1) In three lines:
def list_of_possible_moves(self): # version 2
return [] if self._state != Game.play else\
[index for index, square in enumerate(self._board)
if square == TicTacToe.square_free]
(2) Possible in two lines (i, s instead index, square). Still 80, not 79:
def list_of_possible_moves(self): # version 3
return [] if self._state != Game.play else\
[i for i, s in enumerate(self._board) if s == TicTacToe.square_free]
(3) In the end, I decided to keep that version:
def list_of_possible_moves(self): # version 4 (C++)
if self._state is not Game.play:
return []
list_of_moves = []
for index, square in enumerate(self._board):
if square == TicTacToe.square_free:
list_of_moves.append(index)
return list_of_moves
What do I do wrong? Any better way to write this piece of code?