4

Sorry if it’s dumb question, I am not a programmer. Just wanted to understand how global variables are handled in if elif statement. The global variable x (Boolean) may be updated elsewhere at high frequency So I need the x to be the same in both If and elif conditional statements. Does the underlying code use the same value of x in both statements or does it read the global variable each time, thus meaning it may be different in the two conditional statements?

Hope that makes sense?

E.g.

global x

if x and not status:
    # Do some stuff

elif not x and status:
    # do some other stuff 

This is just a question trying to understand how global variables are handled

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
Mark E
  • 41
  • 1
  • Does this answer your question? https://stackoverflow.com/questions/423379/using-global-variables-in-a-function – Marcelo Paco Mar 31 '23 at 22:22
  • I believe the answer would be compiler-dependent. Some optiomizations could occur on reading the value just once, then using the same register on both tests without re-reading it. Interesting question however. – PoneyUHC Mar 31 '23 at 22:24
  • 1
    Another thing to clear up, you have a `global` statement that doesn't do anything, it is *in the global scope*. But also, even if we imagine we are in a local scope, you don't need `global x` to *merely reference a global variable in a nonglobal scope*. You only need that `global` statement to *assign* to a global variable in a non-global scope. i.e. assignments are by default *local* – juanpa.arrivillaga Mar 31 '23 at 22:28
  • 2
    @PoneyUHC no, absolutly not. That would violate Python's semantics. – juanpa.arrivillaga Mar 31 '23 at 22:29
  • 2
    It would read `x` each time it needs it; if you have another thread updating `x`, you have a *race condition*. You would want to use a lock to ensure that no other thread can update `x` while you are using it. – chepner Mar 31 '23 at 22:47
  • 1
    There's nothing special about `if/elif`. They're no different from any other sequential statements that refer to the same variable. – Barmar Mar 31 '23 at 22:49
  • FWIW, I haven't done much parallel programming, but if you're only reading the value and not changing it in this section of code, the solution could be as simple as `y = x; if y ...`. Otherwise I think you would have to put a lock on any section of code that modifies `x`. – wjandrea Mar 31 '23 at 23:10
  • *"global variable x (Boolean) may be updated elsewhere at high frequency"* This doesn't detract from your question, however this design seems to be a textbook example supporting the, sometimes pretty dogmatic, *global variables are evil* memes. https://stackoverflow.com/questions/19158339/why-are-global-variables-evil – JL Peyret Mar 31 '23 at 23:11
  • @JLPeyret True, although the problem would be the same if they were using some other shared data structure to communicate among the threads. – Barmar Mar 31 '23 at 23:22
  • @Barmar Quite likely. Still, if there is one situation where a global seems inadvisable and more isolation and immutability would be nice this Q seems to personify it. – JL Peyret Mar 31 '23 at 23:25
  • @JLPeyret I think the point of this code is that he wants to have this shared state between the threads. – Barmar Mar 31 '23 at 23:26

2 Answers2

0

As you've written it, your code will load the value x two different times for you in the if and elif conditions, respectively. The global nature of that variable doesn't really factor in to that (it would be the same with local variables in a function).

If you want to only read x once, you can rewrite your compound conditions to instead use an if and else for the test of x, with inner if statements that handle testing status:

if x:
    if not status:
        # do stuff
else:                  # this is the big change, no second test of x here!
    if status:
        # do other stuff
Blckknght
  • 100,903
  • 11
  • 120
  • 169
0

Just throwing this out as a possible approach, with minimal impact on the overall decision structure, if x is a simple variable. copy.deepcopy may work on more complex variables.

This is similar to using y = x when x is immutable, as mentioned in a comment.

However, as remarked in my comment, a design relying on a fast-mutating global variable to drive processing and decisions seems to be asking for trouble.

import copy

global x

#take a copy and then use for this pass 
x2 = copy.copy(x)


if x2 and not status:
    # Do some stuff

elif not x2 and status:
    # do some other stuff 



JL Peyret
  • 10,917
  • 2
  • 54
  • 73