0
import unittest
from paycheck import with_checker
class TestStrings(unittest.TestCase): 
 @with_checker([int])
 def test_sort(self, list_of_ints):
 self.assertTrue(isinstance(list_of_ints, list))
 self.assertTrue(len(list_of_ints) == len(qsort(list_of_ints)))
 self.assertTrue(False)
if __name__ == '__main__':
 unittest.main()
 def qsort (list):
 if list == []:
 return []
 else:
 pivot = list [0]
 lesser = qsort([x for x in list[1:] if x<pivot])
 greater = qsort([x for x in list[1:] if x>=pivot])
 return lesser + [pivot] + greater

This gives me global name qsort not defined (even if i have it in the same class). What is the problem here? I suppose it is simple, but I'm not yet very familiar with Python.

asked Jun 27, 2012 at 6:52
1
  • I guess there is a reason for using your own qsort instead of Python's sorted() Commented Jun 27, 2012 at 7:09

2 Answers 2

3

It looks like you're running the tests before you define the function. Function definitions in Python are executable statements like any other and are executed in the order they're encountered. Since you call unittest.main() before you define qsort, qsort is not defined when the test is run. Move your unittest.main() call after the def qsort block.

While you're at it, it's better not to put the def inside the if __name__=="__main__" block anyway. Usually that if block will be the last bit in your program. Just do

def qsort (list):
 if list == []:
 return []
 else:
 pivot = list [0]
 lesser = qsort([x for x in list[1:] if x<pivot])
 greater = qsort([x for x in list[1:] if x>=pivot])
 return lesser + [pivot] + greater
if __name__ == '__main__':
 unittest.main()

Also note that qsort is not "in the same class" as anything. It's not in any class. It's just a function.

answered Jun 27, 2012 at 6:55
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I think I should read up on functions in python coming from a Java background :)
1

The dedent at if __name__... signifies the end of the class definition. You should move those two lines to the bottom, qsort is now part of your class

import unittest
from paycheck import with_checker
class TestStrings(unittest.TestCase): 
 @with_checker([int])
 def test_sort(self, list_of_ints):
 self.assertTrue(isinstance(list_of_ints, list))
 self.assertTrue(len(list_of_ints) == len(qsort(list_of_ints)))
 self.assertTrue(False)
 def qsort (list):
 if list == []:
 return []
 else:
 pivot = list [0]
 lesser = qsort([x for x in list[1:] if x<pivot])
 greater = qsort([x for x in list[1:] if x>=pivot])
 return lesser + [pivot] + greater
if __name__ == '__main__': # move these two lines
 unittest.main() # to the bottom
answered Jun 27, 2012 at 7:00

4 Comments

If you move qsort to the class you need self when calling it from inside.
@Matthias, True. I won't make those changes here as it doesn't address the immediate question.
Well I guess it somewhat does, as I still get the NameError if i have it on the same indentation level as the test, if i put it at "indentation 0" i get no error and it works,
@Vixen, sure if you put it in the global namespace it will work (and probably makes more sense to do so)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.