refactoring so that multiple changes can be made with one variable?

Paddy paddy3118 at netscape.net
Tue Nov 14 11:34:35 EST 2006


John Salerno wrote:

> My code is below. For now I'm focusing on the lines where health (and
> armor) are increased in each character class. Let's say I decided to
> change the amount of increase in the future. As it is now, I'd have to
> go to each character class and change the number so that each is still
> in a good relation to the other (right now: 3, 2, 1; later: perhaps 4,
> 3, 2, 1, if I added a new class -- i.e., change Fighter from 3 to 4,
> Thief from 2 to 3, in other words increase them all by 1). So instead of
> changing each one, is there a simple, clean way of just changing a
> single number so that this change is reflected in all classes? Hope that
> makes sense.
>
>
You could keep a handle on all object instances created then go through
the objects making appropriate changes, e.g:


class Character(object):
     instances = []
     def __init__(self, name, strength, dexterity, intelligence):
          instances.append(self)
          # as before ...
     def mod_instances(self):
          for inst in instances:
               inst.some_property += 1     # or whatever
# (Untested)

- Paddy.




More information about the Python-list mailing list