473,511 Members | 16,846 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.

class Character(object):
def __init__(self, name, strength, dexterity, intelligence):
self.name = name
self.health = 10
self.armor = self.attack = self.defense = self.magic_attack = \
self.magic_defense = 0
self.strength = strength
self.dexterity = dexterity
self.intelligence = intelligence
self.adjust_attributes()

def adjust_attributes(self):
pass
class Fighter(Character):
def adjust_attributes(self):
self.health += 3
self.armor += 3
self.attack += 2
self.defense += 2
self.strength += 1
class Thief(Character):
def adjust_attributes(self):
self.health += 2
self.armor += 2
self.attack += 1
self.defense += 1
self.magic_defense += 1
self.dexterity += 1
class Mage(Character):
def adjust_attributes(self):
self.health += 1
self.armor += 1
self.magic_attack += 2
self.magic_defense += 2
self.intelligence += 1
Nov 14 '06 #1
6 950

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.

Nov 14 '06 #2
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.
Nov 14 '06 #3
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.

class Character(object):
def __init__(self, name, strength, dexterity, intelligence):
self.name = name
self.health = 10
self.armor = self.attack = self.defense = self.magic_attack = \
self.magic_defense = 0
self.strength = strength
self.dexterity = dexterity
self.intelligence = intelligence
self.adjust_attributes()

def adjust_attributes(self):
pass
class Fighter(Character):
def adjust_attributes(self):
self.health += 3
self.armor += 3
self.attack += 2
self.defense += 2
self.strength += 1
class Thief(Character):
def adjust_attributes(self):
self.health += 2
self.armor += 2
self.attack += 1
self.defense += 1
self.magic_defense += 1
self.dexterity += 1
class Mage(Character):
def adjust_attributes(self):
self.health += 1
self.armor += 1
self.magic_attack += 2
self.magic_defense += 2
self.intelligence += 1
The place to do this seems to be in the Character class.

class Character(object):
_health_base_inc = 1
_armor_base_inc = 1
# etc
def __init__(self, name, strength, dexterity, intelligence):
self.name = name
self.health = 10
self.armor = self.attack = self.defense = self.magic_attack = \
self.magic_defense = 0
self.strength = strength
self.dexterity = dexterity
self.intelligence = intelligence
self.adjust_attributes()

def adjust_attributes(self):
pass

class Mage(Character):
# for symmetry with Character
_health_char_inc = 1
_armor_char_inc = 1
# etc
def adjust_attributes(self):
self.health += self._health_char_inc + self_health_base_inc
self.armor += self._armor_char_inc + self._armor_base_inc
# etc

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Nov 14 '06 #4

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
instances, e.g:

def mod_func1(inst):
inst.abc += inst.xyz # or whatever

class Character(object):
instances = []
def __init__(self, name, strength, dexterity, intelligence):
instances.append(self)
# as before ...
def mod_instances(self, mod_func):
for inst in instances:
mod_func(inst)

You can define different mod_func, like mod_func1 to make whatever
changes to the instances.

Nov 15 '06 #5
On Tue, 14 Nov 2006 10:41:53 -0500, 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.
Cutting your code down to the minimum that exhibits the behaviour you want:

class Character(object):
def __init__(self, name, strength, dexterity, intelligence):
self.name = name
self.health = 10
# and so on...
self.adjust_attributes()
def adjust_attributes(self):
pass
class Fighter(Character):
def adjust_attributes(self):
self.health += 3

class Thief(Character):
def adjust_attributes(self):
self.health += 2

etc.

Sounds like you want some sort of factory function that returns a class:

# WARNING: untested
def make_character_class(name, adjustments):
class klass(Character):
_adjustments = {}
def adjust_attributes(self):
for key, value in self._adjustments.items():
x = getattr(self, key)
setattr(self, key, x + item)

setattr(klass, klass.__name__, name)
setattr(klass, klass._adjustments, adjustments)
return klass
And now you use it like this:

Fighter = make_character_class('Fighter', {'health': 3})
Thief = make_character_class('Thief', {'health': 2})

Now you can easily change the adjustments, all in just a few lines.

Here's another idea:

character_adjustments = { 'Fighter': {'health': 3},
'Thief': {'health': 2},
'Mage': {'intelligence': 3, 'strength': -1}
}

and change the make_character_class factory function above to only take a
single argument, name. Now if you decide you want to *programmatically*
adjust the adjustments, you can do this:

# still untested...
for key in character_adjustments:
for attribute, value in key.items():
# adjust the values to make up for my poor choices
character_adjustments[key][attribute] = value + 1

(but of course you must do this BEFORE creating your character classes!)
And last but most certainly not least, you can separate the adjustment
values into (say) an INI file, read them in at run-time and pass those
values to the factory function above. Then write another function which
walks through the INI file, adjusting the values in place as needed. This
is obviously going to take the most work, so I strongly suggest you don't
go down this path unless you really have to.
--
Steven D'Aprano

Nov 15 '06 #6
On 2006-11-15, Dennis Lee Bieber <wl*****@ix.netcom.comwrote:
On Wed, 15 Nov 2006 18:57:39 +1100, Steven D'Aprano
<st***@REMOVEME.cybersource.com.audeclaimed the following in
comp.lang.python:
>And last but most certainly not least, you can separate the
adjustment values into (say) an INI file, read them in at
run-time and pass those values to the factory function above.
Then write another function which walks through the INI file,
adjusting the values in place as needed. This is obviously
going to take the most work, so I strongly suggest you don't
go down this path unless you really have to.

Nice to see I wasn't the only one to consider extending to an
INI file for this <G>
Coincidentally, I'm just reading "The Pragmattic Programmer" for
the first time. One of guidelines is to pry the details ut of the
code if they might change. The above advice seems like a perfect
example.

--
Neil Cerutti
For those of you who have children and don't know it, we have a
nursery downstairs. --Church Bulletin Blooper
Nov 15 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
7454
by: noah | last post by:
Does PHP have a feature to associate Cookie sessions with a persistent database connection that will allow a single transaction across multiple HTTP requests? Here is how I imagine my process: I...
17
43869
by: Roland Hall | last post by:
Is there a way to return multiple values from a function without using an array? Would a dictionary object work better? -- Roland Hall /* This information is distributed in the hope that it...
22
23319
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
8
2000
by: Frank Rizzo | last post by:
I keep hearing this term thrown around. What does it mean in the context of code? Can someone provide a definition and example using concrete code? Thanks.
32
14762
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
16
1766
by: Richard | last post by:
Hi, I am passing a structure to a subroutine where the passed parameter has been declared as ByVal. However, changes made to the passed variable inside the subroutine flow through to the...
15
4364
by: Simon Cooke | last post by:
Does anyone know of any tools for refactoring header files? We're using a third party codebase at work, and pretty much every file includes a 50Mb precompiled header file. I'm looking for a tool...
7
1108
by: Kamilche | last post by:
''' I'm in the middle of a refactoring dilemma. I have several singletons that I'm turning into modules, for ease of access. The usual method is noted as 'Module 1' below. The new method is...
4
1624
by: shuisheng | last post by:
Dear All, I have a code developed by former employees. I extract some part of it as below: // definition of class CWNPrimitiveFace, it represent a face class CWNPrimitiveFace : public...
0
7355
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7423
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5066
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3225
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1576
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
447
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.