python - Global name is not defined for second unittest -


eclipse platform, python 3.3.

i've created code below demonstrate problem when using global variables , python unittest. i'd know why second unit test (a direct repeat of first) results in

nameerror: global name '_fred' not defined 

try commenting out second test , it'll pass ok.
(note: i've added brief digest of real code is attempting achieve after example, it'll less obtrusive there it's not relevant issue)

''' global problem ''' import unittest  _fred = none  def start():     global _fred     if _fred none:         _fred = 39     _fred += 3  def stop():     global _fred     if _fred not none:         del _fred  class test(unittest.testcase):     def setup(self):         start()      def teardown(self):         stop()      def test_running_first_time(self):         assert(_fred == 42)      def test_running_second_time(self):         assert(_fred == 42)  if __name__ == "__main__":     #import sys;sys.argv = ['', 'test.testname']     unittest.main() 

in real code _fred variable referencing instance of class derived thread (see did there) , gets assigned in start method.
_fred = mythreadclass()
there second global synchronized queue.
methods start , stop control processing queue items on dedicated thread. 'stop' stops processing while allowing items added.
api thread permits single call start. restart processing need new instance of thread. hence use of

if _fred none: 

and

del _fred 

no prizes guessing primary language

del _fred not set _fred none or that. removes name _fred. completely. global, it's if had never existed. local, it's if had never been assigned to. set variable none, obvious thing:

_fred = none 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -