I’m programming a Sudoku solver, and as part of that I’m creating a class to represent a Sudoku board. Part of the functionality of the class is the ability to print itself. Right now I have a method PrintBoard as part of my class. So I would use it like this:
myBoard.PrintBoard()
But I was thinking; is there a way to override python’s print so that:
print(myBoard)
runs the PrintBoard method?
Yes. Have a look at the
__str__method.You can also use the
__repr__method, but as stated in the documentation:From your description, that does not sound like your goal for printing.