[Tutor] Design Question... from a newbie
Alan Gauld
alan.gauld at freenet.co.uk
Wed Oct 20 23:08:46 CEST 2004
> language. I may know how a lot of English words, and how to write a
> sentence but I won't be able to write a novel right a way... the
same
> goes with programming I suppose.
Absolutely. That is one reason I added a chapter on software design
in my book. It doesn't teach design (what in one chapter? Come on! :)
but it explains the need for it...
> class Character:
> def __init__(self, name=' ', job = 1, race = 1):
> self.name = name
> # assign job
> if job == 1:
> self.job = job
> self.jobdesc = "Fighter"
> elif job == 2:
> self.job = class
> self.job = "Mage"
You might find it easier to use dictionaries here (assuming you will
eventually have more than 2 jobs/races)
jobdesc = { 1: 'Fighter', 2: 'Mage' }
class Character:
def __init__...
self.job = job # you may not even need this now
self.jobdesc = jobdesc[job]
It saves an ever growing if/elif chain.
> Would it make more sense to have the class just handle the
attributes
> and do all of the assignment via a separate function? The example I
> gave is very simple.. I imagine as the character information gets
more
> in depth the list of attributes will grow and grow...so I'm
concerned
> that it might start to get confusing. What do you gurus think?
I'd definitely go the way you are moving but use dictionaries.
Also you can start to add methods to characters, for example if you
want
a character to decribe themselves you could add a say() method, or if
characters can move around why not put the move code ina move() method
of the character?
And of course you may decide to have special types of characters with
their own rules, thats where inheritance comes in...
So longer term the class/OOP based approach has a lot more potential.
Alan G.
More information about the Tutor
mailing list