[Tutor] New to this list ....

Russel Winder russel at winder.org.uk
Fri Mar 30 18:10:02 CEST 2012


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 at ekiga.net
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel at winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/tutor/attachments/20120330/f71d0537/attachment.pgp>


More information about the Tutor mailing list