Re: [Tutor] Equivalent 'case' statement

2008-05-24 Thread Dinesh B Vadhia
The dictionary of functions was the way to go and does perform much faster than if/elif's. Thank-you! - Original Message - From: inhahe To: Dinesh B Vadhia Cc: tutor@python.org Sent: Thursday, May 22, 2008 4:15 PM Subject: Re: [Tutor] Equivalent 'case' statement

Re: [Tutor] Equivalent 'case' Statement

2008-05-23 Thread Kinuthia Muchane
Hi, I messed in earlier message, my apologies. Message: 1 Date: Fri, 23 May 2008 00:25:23 +0100 From: "Alan Gauld" <[EMAIL PROTECTED]> Subject: Re: [Tutor] Equivalent 'case' statement To: tutor@python.org Message-ID: <[EMAIL PROTECTED]> Content-Type: text/plain;

Re: [Tutor] Equivalent 'case' statement

2008-05-22 Thread Alan Gauld
"Dinesh B Vadhia" <[EMAIL PROTECTED]> wrote Is there an equivalent to the C/C++ 'case' (or 'switch') statement in Python? No, just if/elif However you can often achieve similar results with a dictionary: def func1(v): return v def func2(v): return v*2 switch = { 'val1': func1,

Re: [Tutor] Equivalent 'case' statement

2008-05-22 Thread inhahe
> > cases = { > 1: lambda: x*2 > 2: lambda: y**2 > 3: lambda: sys.stdout.write("hi\n") > } > i forgot the commas. cases = { 1: lambda: x*2, 2: lambda: y**2, 3: lambda: sys.stdout.write("hi\n") } > > your functions and lambdas can also take parameters of course > lambda a, b: a*b _

Re: [Tutor] Equivalent 'case' statement

2008-05-22 Thread inhahe
no, but you can a) use elifs if c==1: do this elif c==2: do this elif c==3: do this b) make a dictionary of functions (this is faster) def case1: do this def case2: do that def case3: do the other cases = {1: case2, 2: case2, 3:case3} cases[c]() if your functions are one expression you c

[Tutor] Equivalent 'case' statement

2008-05-22 Thread Dinesh B Vadhia
Is there an equivalent to the C/C++ 'case' (or 'switch') statement in Python? Dinesh ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor