[Tutor] playing around with function
Kent Johnson
kent37 at tds.net
Fri Jul 28 12:34:57 CEST 2006
kakada wrote:
> # I have change my code style like this:
>
> def gotoNF(self):
> self.state = "NF"
> self.navigationNext(self.state)
> def gotoNT(self):
> self.state = "NT"
> self.navigationNext(self.state)
> def gotoNU(self):
> self.state = "NU"
> self.navigationNext(self.state)
> def gotoNA(self):
> self.state = "NA"
> self.navigationNext(self.state)
>
> def gotoPA(self):
> self.state = "PA"
> self.navigationPrevious(self.state)
> def gotoPT(self):
> self.state = "PT"
> self.navigationPrevious(self.state)
> def gotoPU(self):
> self.state = "PU"
> self.navigationPrevious(self.state)
> def gotoPF(self):
> self.state = "PF"
> self.navigationPrevious(self.state)
>
All of the above could store a method as well as or instead of a state.
Then you can get rid of the big conditional below.
You don't show the code to create self.store.units so I don't know what
kind of object is there. I'll assume they are instances of class Unit.
Then you could write for example
def gotoPF(self):
self.state = "PF" # You may not need this
self.test = Unit.isfuzzy # This stores a reference to the correct method of Unit
self.navigationPrevious(self.state)
If you change all the gotoXX() methods this way, then navigated becomes just
def navigated(self):
if self.test(self.store.units[int(self.id)]:
return True
Kent
> def navigated(self):
> if (self.state == 'NF') and
> (self.store.units[int(self.id)].isfuzzy()):
> return True
> if (self.state == 'PF') and
> (self.store.units[int(self.id)].isfuzzy()):
> return True
> if (self.state == 'NT') and
> (self.store.units[int(self.id)].istranslated()):
> return True
> if (self.state == 'PT') and
> (self.store.units[int(self.id)].istranslated()):
> return True
> if (self.state == 'NU') and
> (self.store.units[int(self.id)].isuntranslated()):
> return True
> if (self.state == 'PU') and
> (self.store.units[int(self.id)].isuntranslated()):
> return True
> if (self.state == 'NA') and
> (self.store.units[int(self.id)].isapproved()):
> return True
> if (self.state == 'PA') and
> (self.store.units[int(self.id)].isapproved()):
> return True
>
More information about the Tutor
mailing list