简体   繁体   中英

Python 2.7 - Global variable not updating (using functions)

Today i was testing some things i neede for my new code, but i ran into a problem. I reduced the code to the following:

def SomeFunction():
    global Turn
    if Turn == 1:
        #some code
        Turn = 0

    if Turn == 0:
        #some code
        Turn = 1

    print Turn

Turn = 1
for i in range(10):
    SomeFunction()  

The problem i am having is that this function keeps printing 1, ten times. What i want to happen is that it prints 1, then 0, then 1 and so on.

I looked at some existing stack overflow posts, but they all suggest i have to tell python the variable Turn is global inside the function, but as i am doing this already, this is confusing to me.

I do have to use global variables though, so using only local variables is not the solution.

Regards, Harm

The problem is that you are overwriting Turn once and again. Try using

elif Turn == 0:

instead of

if Turn == 0:

If Turn is equal to 1 the first if condition will be True, so Turn will get set to 0. But then execution passes to the second if condition, which is now True , so Turn will get reset to 1.

The sensible way to handle that is to use elif , as others have mentioned. Alternatively, you can duplicate the print into both branches, and put an early return in the first branch. The elif approach is better because it avoids the code duplication, and it's also good style to avoid early return s if you can. But I'll show you the code anyway:

def SomeFunction():
    global Turn
    if Turn == 1:
        #some code
        Turn = 0
        print Turn
        return

    if Turn == 0:
        #some code
        Turn = 1
        print Turn

Turn = 1
for i in range(10):
    SomeFunction()  

output

0
1
0
1
0
1
0
1
0
1

BTW, the usual Python convention is to use lower case for simple variable and function names. Capitalized and CamelCase names are used for class names. Of course, you don't have to follow this convention, but if ypou don't it makes your code look strange when viewed with most syntax highlighting software, so it's unnecessarily confusing to the rest of the Python community.

See PEP 0008 -- Style Guide for Python Code for details.


Actually, you can alternate a value between zero and one without using an if statement. The trick is to use the exclusive-OR operator, ^ :

def SomeFunction():
    global Turn
    Turn ^= 1
    print Turn

Turn = 1
for i in range(10):
    SomeFunction()  

This results in the same output as before.

If a variable only ever takes on the values zero and one you should consider making at a boolean instead, and have it alternate between False and True , as that can lead to more readable code. You can alternate it with:

Turn = not Turn

and you can use a boolean value in arithmetic expressions, where it will behave just like 0 or 1, although some people don't like doing that, and consider it less readable.


I guess I should also mention that you should try to avoid using global . It can be handy, but use of modifiable globals breaks the modularity of code. It's not a big deal for small scripts, but you will really appreciate modular design when you write large complex programs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM