1

So I have a small python program that is spread out across a few classes. In my main class, I tell my title screen class to display and then wait for input. If the input it gets is 'q' it calls back to my main class telling it to set it's stop flag to true. Otherwise, it just loops.

This is the callback I give to my title screen:

def quit():
    stopped = True

stopped is set to False outside of the callback. The callback is registered fine, and goes off no problem, but it seems to set stopped to true locally in titlescreen, and not in main. I can fix this by creating a class stopFlag and doing the exact same thing, except in the object.

My question is why do I need to make a new class to do this? Is there a way I can set a global flag in main which is just a boolean without making an object out of it? How can I have the callback reference that boolean?

Edit:

I declare stopped like this:

stopped = False

Here is the quit callback register call:

titleScreen.registerCallbackQuit(quit)

Which looks like:

def registerCallbackQuit(self, callback):
    self.callbackQuit = callback

And it calls quit if it gets a in the user input.

Andrew T.
  • 4,598
  • 4
  • 35
  • 54
  • 2
    `global stopped` before the assignment, I'd guess. If you'd put the full code, I could give you an answer. http://stackoverflow.com/questions/291978/short-description-of-python-scoping-rules – unddoch Jul 18 '13 at 19:46
  • Assuming `stopped=True` is the only True assignment to stopped in all your code, add `import pdb;pdb.set_trace()` above the line and find out where it is getting called from. – Rian Rizvi Jul 18 '13 at 19:55

1 Answers1

0

global stopped would work (probably). People use classes to avoid globals (among other things). If 'stopped' is spread out over many files, you would need to import it.

Jiminion
  • 5,080
  • 1
  • 31
  • 54