FunnyFunkyBuggy
FunnyFunkyBuggy

Reputation: 613

pass by assignment in python

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.

Upvotes: 1

Views: 829

Answers (3)

Anand S Kumar
Anand S Kumar

Reputation: 90979

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.

Upvotes: 0

yolenoyer
yolenoyer

Reputation: 9445

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.

Upvotes: 1

deets
deets

Reputation: 6395

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

Upvotes: 0

Related Questions