Style Q: Instance variables defined outside of __init__
On Tuesday, March 20, 2018 at 1:10:19 PM UTC-4, Irv Kalb wrote:
> I am aware of all the issues involved. My example code was an attempt to demonstrate the clearest, simplest case possible. My question is not about this small case. In classes designed for full games, I often have a "reset" method that resets many instance variables (and potentially other display fields and graphics) for a new round of playing a game. Grouping this into a method called something like "reset" makes logical sense to me. You tell the game object to reset itself, and it does whatever it needs to do to reset for a new round. My __init__ method calls reset to initialize the first round of the game. This ensures that every play of the game goes through the same initialization.
Calling reset() from __init__() and duplicating resetting logic during initialization aren't the only choices.
My personal preference in such situations is to put all initialization code in __init__. Then write a separate factory function / method to create the new object for the next round. Optionally, this factory can be parametrized by the previous round object.