I have a Game class which has a field that's a Board object. I currently make a Game by passing in a string encoding information about the game and making the Board object.
However, I now have a bunch of Board objects and want to make a new constructor that takes in the Board object directly.
Here's my current constructor:
def __init__(self, game_string):
self.single_list = game_string.split(",")
self.board = self.parse_game_string(game_string)
self.directions = self.get_all_directions()
self.red_number_of_streaks = self.get_number_of_streaks("R")
self.black_number_of_streaks = self.get_number_of_streaks("B")
But now I have the board object, so I'd like to just do:
def __init__(self, board):
self.board = board
self.directions = self.get_all_directions()
self.red_number_of_streaks = self.get_number_of_streaks("R")
self.black_number_of_streaks = self.get_number_of_streaks("B")
I don't think Python will know how to distinguish between these two constructors. I could determine what to do based on the type of argument? Something like:
if isinstance(str): # usual constructor functionality elif isinstance(Game): # new constructor functionality
Is there a better way?
Thanks!