refactoring so that multiple changes can be made with one variable?
paddy3118 at netscape.net
Wed Nov 15 08:10:07 CET 2006
John Salerno wrote:
> Paddy wrote:
> > 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)
> But doesn't this require that the change be predetermined so you can
> code it into the method?
> I don't necessarily need a programmatic way to do this, just a simple
> way to go back to the code and edit a single thing, instead of having to
> update all the numbers.
I just put in a simple version of mod_instances. mod_instances could
take a function as an argument the napply the function to all
inst.abc += inst.xyz # or whatever
instances = 
def __init__(self, name, strength, dexterity, intelligence):
# as before ...
def mod_instances(self, mod_func):
for inst in instances:
You can define different mod_func, like mod_func1 to make whatever
changes to the instances.
More information about the Python-list