[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] Initializing and Reinitializing Instance variables
- To: pygame-users@xxxxxxxx
- Subject: [pygame] Initializing and Reinitializing Instance variables
- From: Irv Kalb <Irv@xxxxxxxxxxxxxx>
- Date: Sat, 25 May 2019 11:29:44 -0700
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: pygame-users-outgoing@xxxxxxxx
- Delivered-to: pygame-users@xxxxxxxx
- Delivery-date: Sat, 25 May 2019 14:37:11 -0400
- In-reply-to: <CAM=hmRPr=S4c--mpTE4HqsPc97L7SKo9n8Mr4iAXRFpJ+Y0=9g@mail.gmail.com>
- References: <CACB9+z3YgcwjK6MLWjH+D-3kt1_1tuUkSr=kpCpuKY1X8PvbGw@mail.gmail.com> <CAJ82j0_4cKfozmHwg=PsiObNsOa3-J2OxF5eE1dYf2nMWCrkkA@mail.gmail.com> <CAH7=JufX4nWyCrTDx6rHq+PwF=cMPFv8soyZkmYp56=uiLH92g@mail.gmail.com> <CAG4NV=ufMxMdKqkU36zvSSTcMj5Prz15RbrNOVzwvy2xhQggYg@mail.gmail.com> <5CBFA08B.2040807@canterbury.ac.nz> <CAM=hmRPr=S4c--mpTE4HqsPc97L7SKo9n8Mr4iAXRFpJ+Y0=9g@mail.gmail.com>
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
Hi,
I am teaching an Python object-oriented programming class based on my own materials. I am using Pygame to explain many OOP concepts. So far its going very well.
I have built many small games as examples, but I keep running into a design dilemma. I will often have a class, where I have an instance variable (I know it should be called an attribute!), and I want to be able to give it a starting value, but I also want to be able to reset it to the same value if the game is played more than once. As a trivial example, here is the start of a Game class, that keeps track of the score of the game:
class Game:
def __init__(self):
self.score = 0
def reset(self):
self.score = 0
My main code instantiates a Game object and the game plays. If the user wants to start the game over, I call the reset method. There are two different problems with this approach:
1) I am duplicating code. This gets nasty when I have lots of instance variables to reset.
2) Any time I want to change the "starting" value, I must make the identical change in both methods.
The obvious solution is to have the __init__ method call the reset method:
class Game:
def __init__(self):
self.reset()
def reset(self):
self.score = 0
That does work fine and I like it. But it breaks a general rule that you should declare all instance variables in the __init__ method. I use PyCharm for the development of these games, and the "linter" (over the right side scroll bar) always flags this as a warning that a variable like this is not defined in my __init__ method.
I'm looking for a general solution that solves this. The best I've come up with so far is to do something like this for each instance variable:
class Game:
def __init__(self):
self.score = None # create the variable here, but only assign it a value in the reset method below
self.reset()
def reset(self):
self.score = 0 # Supply the actual start value here.
Anyone have any other suggestions for how to do this cleanly?
Thanks,
Irv