Message info
 
To:Barry Drake From:Russel Winder Subject:Re: [Tutor] New to this list .... Date:Fri, 30 Mar 2012 17:10:02 +0100
 

Barry,

On Fri, 2012-03-30 at 16:42 +0100, Barry Drake wrote:
[...]
> def getflag(thisflag, results):
> if (thisflag == 2):
> results[0] += 1
> elif (thisflag == 1):
> results[1] += 1
> elif (thisflag == 0):
> results[2] += 1
> return(results)

Two thoughts spring to mind:

-- is it possible to arrange the data model such that the value of the
thisflag is the index into the sequence, then:

def getflag(thisflag, results):
results[thisflag] += 1
return results

-- seriously "overengineered" for this particular example but the
replacement for switch statement is a dictionary and function pointers.

from functools import partial

def alt(thisflag, results):
def setflag(x):
results[x] += 1
{
0: partial(setflag, 2),
1: partial(setflag, 1),
2: partial(setflag, 0),
}[thisflag]()
return results

--
Russel.
=============================================================================
Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder@ekiga.net
41 Buckmaster Road m: +44 7770 465 077 xmpp: russel@winder.org.uk
London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder