How to optimise this code?

kyosohma at gmail.com kyosohma at gmail.com
Tue Aug 21 12:14:02 EDT 2007


On Aug 21, 10:59 am, "David N Montgomery" <monty.pyt... at fastmail.fm>
wrote:
> class testCase:
>     def __init__(self, tc):
>         if tc == 1:self.testCase1()
>         if tc == 2:self.testCase2()
>         if tc == 3:self.testCase3()
>         if tc == 4:self.testCase4()
>         if tc == 5:self.testCase5()
>         if tc == 6:self.testCase6()
>
>     def testCase1(self):
>         print "tc1"
>
>     def testCase2(self):
>         print "tc2"
>
>     def testCase3(self):
>         print "tc3"
>
>     def testCase4(self):
>         print "tc4"
>
>     def testCase5(self):
>         print "tc5"
>
>     def testCase6(self):
>         print "tc6"
>
>     def testCaseX(self):
>         print "tcX"
>
> totalNumberOfTestCases = 6
> x = 0
> while x <= totalNumberOfTestCases:
>     x += 1
>     testCase(x)
>
> This template code is working, but I envisage having 100+ test cases and
> am concerned about my useage of if statements. I would be grateful for
> any pointers as to how I can run all tests cases, regardless of how
> many, in a more efficient manner.
>
> Thank you in advance.

You're code doesn't make sense to me. You create a class and then you
call a method within the class without instantiating said class. What
the!?

Can't you just create a function?

<code>
def testCase(x):
    print "tc%s" % x

totalNumberOfTestCases = 6
x = 0
while x <= totalNumberOfTestCases:
    x += 1
    testCase(x)

</code>

Am I missing something?

Mike




More information about the Python-list mailing list