1

I know this question has been asked by several times but I still can't find the solution for my problem.

I was writing a function to change something outside the function (in this case, a boolean variable). Although it is known that boolean variables are mutable, I still can't change it. This is my code :

def test() :
 global cont
 do_something = True #Actually it was something else, but for easy reading, I set it as True
 if do_something :
 cont = False
def main() :
 cont = True
 while cont :
 test()
 print "Stop me"
print "HI"
main()
print "HI"

It simply ran into an infinite loop. I know the following code works.

def test() :
 global cont
 do_something = True #Actually it was something else, but for easy reading, I set it as True
 if do_something :
 cont = False
cont = True
print "HI"
while cont :
 test()
 print "Stop me"
print "HI"

Is this something to do with the global label ? I was told that if I set something global, I can use it anywhere in my program. Is this a special case? So, how can I modify my code to be functional (able to change the "cont" variable) Thanks.

asked Jul 18, 2015 at 16:16
3
  • 1
    you also need global const in main Commented Jul 18, 2015 at 16:19
  • 1
    Which is true, but you should not be doing this. Pass the variable in and return it. Also, booleans are not mutable. Commented Jul 18, 2015 at 16:20
  • I found this Pycon talk pretty useful for explaining how python variables and scope work Commented Jul 18, 2015 at 16:22

3 Answers 3

1

Also add the global statement in the main function:

def main() :
 global cont
 cont = True
 while cont :
 test()
 print "Stop me"

In Python, if you assign a variable inside a function, and you don't add the global statement for this variable, it will always be considered as a local variable.

answered Jul 18, 2015 at 16:20
Sign up to request clarification or add additional context in comments.

1 Comment

Nitpick: the "before" is irrelevant. If you assign to a name anywhere in a function, that name is considered local to that function (unless it's been declared as global or nonlocal, of course).
0

You should avoid using global. The reason is that you create code which is extremely hard to reason about - as code all over the place can change, and thus influence the behaviour of code you currently try to debug or change.

So for your given problem, the real solution is to use a return-value to communicate between main and test. I augmented the example to illustrate how you can use several values as returns, to show how to apply the pattern in cases where there is both a value the function should return, plus the "meta" information that the caller should continue

def test():
 do_continue = True # both somehow computed
 value = 1000
 return do_continue, value
def main():
 while True:
 do_continue, result = test()
 if not do_continue:
 break
answered Jul 18, 2015 at 16:26

Comments

0

In your original code , the cont inside main() function is a local variable, not a global variable, so even though you change the global variable cont inside test() , it does not reflect in the local variable cont in main() .

You have to make the cont variable in main() global as well for your case.

def test() :
 global cont
 do_something = True #Actually it was something else, but for easy reading, I set it as True
 if do_something :
 cont = False
def main() :
 global cont
 cont = True
 while cont :
 test()
 print "Stop me"
print "HI"
main()
print "HI"

Please note, you should avoid using global variables - Why are global variables evil?

And I believe they are costly in python.

answered Jul 18, 2015 at 16:20

Comments

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.